diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt index 07cc3730..f6a181b7 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt @@ -59,21 +59,22 @@ abstract class MavenPublishBaseExtension( val buildService = project.registerSonatypeRepositoryBuildService( sonatypeHost = sonatypeHost, + groupId = groupId, + versionIsSnapshot = version.map { it.endsWith("-SNAPSHOT") }, repositoryUsername = project.providers.gradleProperty("mavenCentralUsername"), repositoryPassword = project.providers.gradleProperty("mavenCentralPassword"), automaticRelease = automaticRelease, ) - val versionIsSnapshot = version.map { it.endsWith("-SNAPSHOT") } - val createRepository = project.tasks.registerCreateRepository(groupId, versionIsSnapshot, buildService) - val url = sonatypeHost.map { it.publishingUrl(versionIsSnapshot, buildService, project.configurationCache()) } - + val configCacheEnabled = project.configurationCache() project.gradlePublishing.repositories.maven { repo -> repo.name = "mavenCentral" - repo.setUrl(url) + repo.setUrl(buildService.map { it.publishingUrl(configCacheEnabled) }) repo.credentials(PasswordCredentials::class.java) } + val createRepository = project.tasks.registerCreateRepository(buildService) + project.tasks.withType(PublishToMavenRepository::class.java).configureEach { publishTask -> if (publishTask.name.endsWith("ToMavenCentralRepository")) { publishTask.dependsOn(createRepository) diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/SonatypeHost.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/SonatypeHost.kt index c6656dea..b0eacb68 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/SonatypeHost.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/SonatypeHost.kt @@ -1,8 +1,6 @@ package com.vanniktech.maven.publish -import com.vanniktech.maven.publish.sonatype.SonatypeRepositoryBuildService import java.io.Serializable -import org.gradle.api.provider.Provider /** * Describes the various hosts for Sonatype OSSRH. Depending on when a user signed up with Sonatype @@ -17,31 +15,6 @@ data class SonatypeHost( return "$rootUrl/service/local/" } - internal fun publishingUrl( - snapshot: Provider, - buildService: Provider, - configCache: Boolean, - ): String { - return if (snapshot.get()) { - require(buildService.get().stagingRepositoryId == null) { - "Staging repositories are not supported for SNAPSHOT versions." - } - "$rootUrl/content/repositories/snapshots/" - } else { - val stagingRepositoryId = buildService.get().stagingRepositoryId - requireNotNull(stagingRepositoryId) { - if (configCache) { - "Publishing releases to Maven Central is not supported yet with configuration caching enabled, because of " + - "this missing Gradle feature: https://github.com/gradle/gradle/issues/22779" - } else { - "The staging repository was not created yet. Please open a bug with a build scan or build logs and stacktrace" - } - } - - "$rootUrl/service/local/staging/deployByRepositoryId/$stagingRepositoryId/" - } - } - companion object { @JvmStatic fun valueOf(sonatypeHost: String): SonatypeHost = when (sonatypeHost) { diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/CreateSonatypeRepositoryTask.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/CreateSonatypeRepositoryTask.kt index 163f671a..5112f741 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/CreateSonatypeRepositoryTask.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/CreateSonatypeRepositoryTask.kt @@ -31,35 +31,19 @@ internal abstract class CreateSonatypeRepositoryTask : DefaultTask() { val workQueue: WorkQueue = getWorkerExecutor().noIsolation() workQueue.submit(CreateStagingRepository::class.java) { requireNotNull(it) - it.projectGroup.set(projectGroup) - it.versionIsSnapshot.set(versionIsSnapshot) it.buildService.set(buildService) } } internal interface CreateStagingRepositoryParameters : WorkParameters { - val projectGroup: Property - val versionIsSnapshot: Property val buildService: Property } abstract class CreateStagingRepository : WorkAction { override fun execute() { val parameters = requireNotNull(parameters) - if (parameters.versionIsSnapshot.get()) { - return - } - val service = parameters.buildService.get() - - // if repository was already created in this build this is a no-op - val currentStagingRepositoryId = service.stagingRepositoryId - if (currentStagingRepositoryId != null) { - return - } - - val id = service.nexus.createRepositoryForGroup(parameters.projectGroup.get()) - service.stagingRepositoryId = id + service.createStagingRepository() } } @@ -67,15 +51,11 @@ internal abstract class CreateSonatypeRepositoryTask : DefaultTask() { private const val NAME = "createStagingRepository" fun TaskContainer.registerCreateRepository( - projectGroup: Provider, - versionIsSnapshot: Provider, buildService: Provider, ): TaskProvider { return register(NAME, CreateSonatypeRepositoryTask::class.java) { it.description = "Create a staging repository on Sonatype OSS" it.group = "release" - it.projectGroup.set(projectGroup) - it.versionIsSnapshot.set(versionIsSnapshot) it.buildService.set(buildService) it.usesService(buildService) } diff --git a/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/SonatypeRepositoryBuildService.kt b/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/SonatypeRepositoryBuildService.kt index 8c65bc0b..2f765c3e 100644 --- a/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/SonatypeRepositoryBuildService.kt +++ b/plugin/src/main/kotlin/com/vanniktech/maven/publish/sonatype/SonatypeRepositoryBuildService.kt @@ -23,6 +23,8 @@ internal abstract class SonatypeRepositoryBuildService : internal interface Params : BuildServiceParameters { val sonatypeHost: Property + val groupId: Property + val versionIsSnapshot: Property val repositoryUsername: Property val repositoryPassword: Property val automaticRelease: Property @@ -42,10 +44,7 @@ internal abstract class SonatypeRepositoryBuildService : ) } - // should only be accessed from CreateSonatypeRepositoryTask - // for all other use cases use MavenPublishBaseExtension - // the id of the staging repository that was created during this build - var stagingRepositoryId: String? = null + private var stagingRepositoryId: String? = null set(value) { if (field != null) { throw IllegalStateException("stagingRepositoryId was already set") @@ -63,6 +62,41 @@ internal abstract class SonatypeRepositoryBuildService : private var buildIsSuccess: Boolean = true + /** + * Is only be allowed to be called from task actions. + */ + fun createStagingRepository() { + if (parameters.versionIsSnapshot.get()) { + return + } + + if (stagingRepositoryId != null) { + return + } + + this.stagingRepositoryId = nexus.createRepositoryForGroup(parameters.groupId.get()) + } + + internal fun publishingUrl(configCacheEnabled: Boolean): String { + return if (parameters.versionIsSnapshot.get()) { + require(stagingRepositoryId == null) { + "Staging repositories are not supported for SNAPSHOT versions." + } + "${parameters.sonatypeHost.get().rootUrl}/content/repositories/snapshots/" + } else { + val stagingRepositoryId = requireNotNull(stagingRepositoryId) { + if (configCacheEnabled) { + "Publishing releases to Maven Central is not supported yet with configuration caching enabled, because of " + + "this missing Gradle feature: https://github.com/gradle/gradle/issues/22779" + } else { + "The staging repository was not created yet. Please open a bug with a build scan or build logs and stacktrace" + } + } + + "${parameters.sonatypeHost.get().rootUrl}/service/local/staging/deployByRepositoryId/$stagingRepositoryId/" + } + } + override fun onFinish(event: FinishEvent) { if (event.result is FailureResult) { buildIsSuccess = false @@ -92,6 +126,8 @@ internal abstract class SonatypeRepositoryBuildService : fun Project.registerSonatypeRepositoryBuildService( sonatypeHost: Provider, + groupId: Provider, + versionIsSnapshot: Provider, repositoryUsername: Provider, repositoryPassword: Provider, automaticRelease: Boolean, @@ -105,6 +141,8 @@ internal abstract class SonatypeRepositoryBuildService : val service = gradle.sharedServices.registerIfAbsent(NAME, SonatypeRepositoryBuildService::class.java) { it.maxParallelUsages.set(1) it.parameters.sonatypeHost.set(sonatypeHost) + it.parameters.groupId.set(groupId) + it.parameters.versionIsSnapshot.set(versionIsSnapshot) it.parameters.repositoryUsername.set(repositoryUsername) it.parameters.repositoryPassword.set(repositoryPassword) it.parameters.automaticRelease.set(automaticRelease)