Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to io.github.gradle-nexus:publish-plugin #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions checksum.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<dependency group='gradle.plugin.org.jetbrains.gradle.plugin.idea-ext' module='gradle-idea-ext' version='0.7'>
<sha512>A85249BA2BB5713B3779B88F3593CA60EA8739860327DAACE205057F811BAA330E772E4DD4B7BC42BBBFA589ACE38210AAFB0CF2919F5691794E6D9C5C505DAD</sha512>
</dependency>
<dependency group='io.github.gradle-nexus' module='publish-plugin' version='0.1.0-SNAPSHOT'>
<sha512>A23044E25918FC08882627DB5C3BCEE3E19F2876952C6D0A08848AA1269DA4DB293326441901661B217AC227814B463A1C1660ABC11026A08ECD45938D6DF775</sha512>
</dependency>
<dependency group='net.sf.ezmorph' module='ezmorph' version='1.0.6'>
<sha512>16D30BE564723B74F312B4E7D06F349370FB6726B3162778C869CD723ECA2A40C4972C2757B3E107E1820CEC0D70B0BD2B96EFCD466518FC64495F7AEF97967A</sha512>
</dependency>
Expand Down
11 changes: 9 additions & 2 deletions plugins/stage-vote-release-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ dependencies {
implementation("org.eclipse.jgit:org.eclipse.jgit:5.6.1.202002131546-r")
implementation("org.ajoberstar.grgit:grgit-gradle:4.0.1")
implementation("org.ajoberstar.grgit:grgit-core:4.0.1")
implementation("de.marcphilipp.gradle:nexus-publish-plugin:0.4.0")
implementation("io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.21.2")
implementation("io.github.gradle-nexus:publish-plugin:0.1.0-SNAPSHOT")
}

repositories {
maven("https://oss.sonatype.org/content/repositories/snapshots/") {
mavenContent {
snapshotsOnly()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import com.github.vlsi.gradle.properties.dsl.props
import com.github.vlsi.gradle.release.svn.LsDepth
import com.github.vlsi.gradle.release.svn.Svn
import com.github.vlsi.gradle.release.svn.SvnEntry
import de.marcphilipp.gradle.nexus.InitializeNexusStagingRepository
import de.marcphilipp.gradle.nexus.NexusPublishExtension
import io.codearte.gradle.nexus.NexusStagingExtension
import io.codearte.gradle.nexus.NexusStagingPlugin
import io.github.gradlenexus.publishplugin.CloseNexusStagingRepository
import io.github.gradlenexus.publishplugin.InitializeNexusStagingRepository
import io.github.gradlenexus.publishplugin.NexusPublishExtension
import io.github.gradlenexus.publishplugin.ReleaseNexusStagingRepository
import org.ajoberstar.grgit.Grgit
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.ObjectId
Expand All @@ -48,6 +48,7 @@ import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.invoke
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.the
Expand All @@ -58,6 +59,7 @@ import org.gradle.plugins.signing.SigningPlugin
import java.io.File
import java.net.URI
import java.nio.charset.StandardCharsets
import java.time.Duration
import javax.inject.Inject

class StageVoteReleasePlugin @Inject constructor(private val instantiator: Instantiator) :
Expand Down Expand Up @@ -122,8 +124,7 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta

private fun Project.configureRoot() {
apply(plugin = "org.ajoberstar.grgit")
apply(plugin = "io.codearte.nexus-staging")
apply(plugin = "de.marcphilipp.nexus-publish")
apply(plugin = "io.github.gradle-nexus.publish-plugin")

val releaseFilesConfiguration = configurations[RELEASE_FILES_CONFIGURATION_NAME]
val releaseSignaturesConfiguration = configurations[RELEASE_SIGNATURES_CONFIGURATION_NAME]
Expand All @@ -141,16 +142,14 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
val validateRcIndexSpecified = tasks.register(VALIDATE_RC_INDEX_SPECIFIED_TASK_NAME)
val validateBeforeBuildingReleaseArtifacts = tasks.register(VALIDATE_BEFORE_ARTIFACT_BUILD_TASK_NAME)

configureNexusPublish(validateNexusCredentials, validateBeforeBuildingReleaseArtifacts)

configureNexusStaging(releaseExt)
configureNexusPublish(releaseExt, validateNexusCredentials, validateBeforeBuildingReleaseArtifacts)

tasks.named("init").hide()
hideMavenPublishTasks()

// Tasks from NexusStagingPlugin
val closeRepository = tasks.named("closeRepository")
val releaseRepository = tasks.named("releaseRepository")
val closeRepository = tasks.named("close${REPOSITORY_NAME.capitalize()}StagingRepository")
val releaseRepository = tasks.named("release${REPOSITORY_NAME.capitalize()}StagingRepository")

val pushRcTag = createPushRcTag(releaseExt, validateRcIndexSpecified, validateBeforeBuildingReleaseArtifacts, closeRepository)
val pushReleaseTag = createPushReleaseTag(releaseExt, validateRcIndexSpecified, releaseRepository)
Expand Down Expand Up @@ -185,8 +184,11 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
}
closeRepository.hide()
releaseRepository.hide()
tasks.named("closeAndReleaseRepository").hide()
tasks.named("getStagingProfile").hide()
// https://github.com/gradle-nexus/publish-plugin/issues/51
afterEvaluate {
tasks.named("closeAndReleaseStagingRepository").hide()
}
tasks.named("closeAndRelease${REPOSITORY_NAME.capitalize()}StagingRepository").hide()

releaseRepository {
// Note: publishSvnDist might fail, and it is easier to rollback than "rollback Nexus"
Expand All @@ -195,15 +197,6 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
dependsOn(validateNexusCredentials)
}

// closeRepository does not wait all publications by default, so we add that dependency
allprojects {
plugins.withType<PublishingPlugin> {
closeRepository.configure {
dependsOn(tasks.named(PublishingPlugin.PUBLISH_LIFECYCLE_TASK_NAME))
}
}
}

pushRcTag {
mustRunAfter(stageSvnDist)
mustRunAfter(closeRepository)
Expand All @@ -218,12 +211,26 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
dependsOn(closeRepository)
}

tasks.register(REMOVE_STALE_ARTIFACTS_TASK_NAME, RemoveStaleArtifactsTask::class) {
// Make sure stageDist depends on all the relevant publish tasks
allprojects {
plugins.withType<MavenPublishPlugin> {
stageDist {
dependsOn(tasks.named("publishAllPublicationsTo${REPOSITORY_NAME.capitalize()}Repository"))
}
}
}

val removeStaleArtifacts = tasks.register(REMOVE_STALE_ARTIFACTS_TASK_NAME, RemoveStaleArtifactsTask::class) {
description = "Removes stale artifacts from dist.apache.org (dry run with -PasfDryRun)"
group = RELEASE_GROUP
onlyIf { releaseExt.svnDistEnabled.get() }
mustRunAfter(publishSvnDist)
}
afterEvaluate {
if (!releaseExt.svnDistEnabled.get()) {
removeStaleArtifacts.hide()
}
}

pushReleaseTag {
mustRunAfter(publishSvnDist)
Expand Down Expand Up @@ -369,11 +376,15 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta

private fun Project.hideMavenPublishTasks() {
allprojects {
plugins.withType<PublishingPlugin> {
tasks.named(PublishingPlugin.PUBLISH_LIFECYCLE_TASK_NAME).hide()
}
plugins.withType<MavenPublishPlugin> {
afterEvaluate {
tasks.withType<PublishToMavenRepository>().hide()
tasks.withType<PublishToMavenLocal>().hide()
tasks.withType<GenerateModuleMetadata>().hide()
tasks.named("publishAllPublicationsTo${REPOSITORY_NAME.capitalize()}Repository").hide()
val generatePomTasks = tasks.withType<GenerateMavenPom>()
generatePomTasks.hide()
tasks.register("generatePom") {
Expand Down Expand Up @@ -511,44 +522,6 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
return pushPreviewSite
}

private fun Project.configureNexusStaging(releaseExt: ReleaseExtension) {
plugins.withType<NexusStagingPlugin> {
tasks {
// Hide "deprecated" tasks from "./gradlew tasks"
named("closeAndPromoteRepository").hide()
named("promoteRepository").hide()
// Hide "internal" tasks
named("createRepository").hide()
}
}
// The fields of releaseExt are not configured yet (the extension is not yet used in build scripts),
// so we populate NexusStaging properties after the project is configured
afterEvaluate {
configure<NexusStagingExtension> {
val nexus = project.the<ReleaseExtension>().nexus
packageGroup = nexus.packageGroup.get()
username = nexus.credentials.username(project)
password = nexus.credentials.password(project)
stagingProfileId = nexus.stagingProfileId.orNull
delayBetweenRetriesInMillis = 2000
numberOfRetries = (releaseExt.nexus.operationTimeout.get().toMillis() / 2000).toInt()
if (releaseExt.release.get()) {
repositoryDescription =
"Release ${releaseExt.componentName.get()} ${releaseExt.releaseTag.get()} (${releaseExt.rcTag.orNull ?: ""})"
}
val nexusPublish = project.the<NexusPublishExtension>()
val repo = nexusPublish.repositories[REPOSITORY_NAME]
serverUrl =
repo.run { if (nexusPublish.useStaging.get()) nexusUrl else snapshotRepositoryUrl }
.get().toString()

stagingRepositoryId.set(
project.provider { releaseExt.repositoryIdStore.getOrLoad(REPOSITORY_NAME) }
)
}
}
}

private fun URI.replacePath(path: String) = URI(
scheme,
userInfo,
Expand All @@ -560,70 +533,46 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
)

private fun Project.configureNexusPublish(
releaseExt: ReleaseExtension,
validateNexusCredentials: TaskProvider<*>,
validateBeforeBuildingReleaseArtifacts: TaskProvider<*>
) {
val releaseExt = project.the<ReleaseExtension>()
val nexusPublish = the<NexusPublishExtension>()
val repo = nexusPublish.repositories.create(REPOSITORY_NAME) {
nexusUrl.set(releaseExt.nexus.url.map { it.replacePath("/service/local/") })
snapshotRepositoryUrl.set(releaseExt.nexus.url.map { it.replacePath("/content/repositories/snapshots/") })
username.set(project.provider { releaseExt.nexus.credentials.username(project) })
password.set(project.provider { releaseExt.nexus.credentials.password(project) })
}

nexusPublish.connectTimeout.set(releaseExt.nexus.connectTimeout)
nexusPublish.clientTimeout.set(releaseExt.nexus.operationTimeout)

val rootInitStagingRepository = tasks.named("initialize${repo.name.capitalize()}StagingRepository")
// Use the same settings for all subprojects that apply MavenPublishPlugin
subprojects {
plugins.withType<MavenPublishPlugin> {
apply(plugin = "de.marcphilipp.nexus-publish")

configure<NexusPublishExtension> {
connectTimeout.set(nexusPublish.connectTimeout)
clientTimeout.set(nexusPublish.clientTimeout)
repositories.create(repo.name) {
nexusUrl.set(repo.nexusUrl)
snapshotRepositoryUrl.set(repo.snapshotRepositoryUrl)
username.set(repo.username)
password.set(repo.password)
}
useStaging.set(nexusPublish.useStaging)
}
configure<NexusPublishExtension> {
val repo = repositories.create(REPOSITORY_NAME) {
nexusUrl.set(releaseExt.nexus.url.map { it.replacePath("/service/local/") })
snapshotRepositoryUrl.set(releaseExt.nexus.url.map { it.replacePath("/content/repositories/snapshots/") })
username.set(project.provider { releaseExt.nexus.credentials.username(project) })
password.set(project.provider { releaseExt.nexus.credentials.password(project) })
}
plugins.withId("de.marcphilipp.nexus-publish") {
tasks.withType<InitializeNexusStagingRepository>().configureEach {
// Allow for some parallelism, so the staging repository is created by the root task
dependsOn(rootInitStagingRepository)
}
connectTimeout.set(releaseExt.nexus.connectTimeout)
clientTimeout.set(releaseExt.nexus.operationTimeout)
val nexus = releaseExt.nexus
packageGroup.set(nexus.packageGroup)
transitionCheckOptions {
delayBetween.set(Duration.ofSeconds(10))
maxRetries.set(nexus.operationTimeout.map { (1 + it.toSeconds() / 10).toInt() })
}
}

// We don't know which project will be the first to initialize the staging repository,
// so we watch all the projects
// The goal of this block is to fetch and save the Id of newly created staging repository
allprojects {
plugins.withId("de.marcphilipp.nexus-publish") {
// Hide unused task: https://github.com/marcphilipp/nexus-publish-plugin/issues/14
configure<NexusPublishExtension> {
repositories.whenObjectAdded {
tasks.named("publishTo${name.capitalize()}") {
group = null
}
}
repositoryDescription.set(project.provider {
if (releaseExt.release.get()) {
project.run { "$group:$name:$version" }
} else {
"Release ${releaseExt.componentName.get()} ${releaseExt.releaseTag.get()} (${releaseExt.rcTag.orNull ?: ""})"
}
tasks.withType<InitializeNexusStagingRepository>().configureEach {
dependsOn(validateBeforeBuildingReleaseArtifacts)
dependsOn(validateNexusCredentials)
doLast {
// nexus-publish puts stagingRepositoryId to NexusStagingExtension so we get it from there
val repoName = this@configureEach.repositoryName.get()
val stagingRepositoryId =
rootProject.the<NexusStagingExtension>().stagingRepositoryId.get()
releaseExt.repositoryIdStore[repoName] = stagingRepositoryId
}
})

tasks.named<ReleaseNexusStagingRepository>("release${REPOSITORY_NAME.capitalize()}StagingRepository") {
stagingRepositoryId.set(project.provider { releaseExt.repositoryIdStore.getOrLoad(REPOSITORY_NAME) })
}
tasks.named<InitializeNexusStagingRepository>("initialize${repo.name.capitalize()}StagingRepository") {
dependsOn(validateBeforeBuildingReleaseArtifacts)
dependsOn(validateNexusCredentials)
doLast {
val repoName = repository.get().name
val closeRepoTask =
rootProject.tasks.named<CloseNexusStagingRepository>("close${repoName.capitalize()}StagingRepository")
val stagingRepositoryId =
closeRepoTask.get().stagingRepositoryId.get()
releaseExt.repositoryIdStore[repoName] = stagingRepositoryId
}
}
}
Expand Down