Skip to content

Commit

Permalink
Gradle 6.2 as minimum (re-merge of #251) (#274)
Browse files Browse the repository at this point in the history
* Bump minimum version to Gradle 6.1

* Use build service all the way (Gradle 6.1)

* Bump minimum version to Gradle 6.2

* Use providers.gradleProperty (Gradle 6.2)

* forUseAtConfigurationTime compatibility

* Use full plugin name in error message

Co-authored-by: Róbert Papp <[email protected]>

* Revert "Use providers.gradleProperty (Gradle 6.2)" to not run into #267

This reverts commit f658356

---------

Co-authored-by: Marcin Zajączkowski <[email protected]>
  • Loading branch information
TWiStErRob and szpak authored Jan 9, 2024
1 parent 06c6953 commit ecfc17e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/java-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Reduce number of Gradle regressions builds
run: |
# Keep only the last Gradle version per Stutter matrix; for each line this transformation is done:
# java11=6.0.1,6.9.4,7.0.2,7.6.1,8.0.2,8.1.1,8.2-rc-1
# java11=6.2.2,6.9.4,7.0.2,7.6.1,8.0.2,8.1.1,8.2-rc-1
# ->
# java11=8.2-rc-1
# The trick is that \2 will greedily eat everything before the last comma.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This plugin is intended as a replacement of the [Gradle Nexus Staging Plugin](ht

### Applying the plugin

The plugin must be applied to the root project and requires Gradle 6.0 or later. It is important to
The plugin must be applied to the root project and requires Gradle 6.2 or later. It is important to
set the group and the version to the root project, so the plugin can detect if it is a snapshot
version or not in order to select the correct repository where artifacts will be published.

Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ stutter {
languageVersion = JavaLanguageVersion.of(8)
}
gradleVersions {
compatibleRange("6.0")
compatibleRange("6.2")
}
}
register("java11") {
javaToolchain {
languageVersion = JavaLanguageVersion.of(11)
}
gradleVersions {
compatibleRange("6.0")
compatibleRange("6.2")
}
}
register("java17") {
Expand Down Expand Up @@ -165,7 +165,7 @@ sourceSets {
}

kotlin.target.compilations.configureEach {
// Supporting Gradle 6.0+ needs to use Kotlin 1.3.
// Supporting Gradle 6.2+ needs to use Kotlin 1.3.
// See https://docs.gradle.org/current/userguide/compatibility.html
// For future maintainer: Kotlin 1.9.0 dropped support for Kotlin 1.3, it'll only support 1.4+.
// This means Gradle 7.0 will be the lowest supportable version for plugins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.github.gradlenexus.publishplugin

import io.github.gradlenexus.publishplugin.internal.InvalidatingStagingRepositoryDescriptorRegistry
import io.github.gradlenexus.publishplugin.internal.StagingRepositoryDescriptorRegistryBuildService
import io.github.gradlenexus.publishplugin.internal.determineStagingProfileId
import org.gradle.api.Incubating
import org.gradle.api.provider.Property
Expand All @@ -29,7 +29,8 @@ import org.gradle.api.tasks.TaskAction
abstract class FindStagingRepository : AbstractNexusStagingRepositoryTask() {

@get:Internal
abstract val registry: Property<InvalidatingStagingRepositoryDescriptorRegistry>
// TODO use @ServiceReference instead of @Internal when minimum is Gradle 8.0.
abstract val registry: Property<StagingRepositoryDescriptorRegistryBuildService>

@get:Optional
@get:Input
Expand Down Expand Up @@ -58,6 +59,6 @@ abstract class FindStagingRepository : AbstractNexusStagingRepositoryTask() {
val descriptor = client.findStagingRepository(stagingProfileId, Regex(descriptionRegex))
logger.lifecycle("Staging repository for {} at {}, stagingProfileId '{}', descriptionRegex '{}' is '{}'", repository.name, serverUrl, stagingProfileId, descriptionRegex, descriptor.stagingRepositoryId)
stagingRepositoryId.set(descriptor.stagingRepositoryId)
registry.get()[repository.name] = descriptor
registry.get().registry[repository.name] = descriptor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.github.gradlenexus.publishplugin

import io.github.gradlenexus.publishplugin.internal.InvalidatingStagingRepositoryDescriptorRegistry
import io.github.gradlenexus.publishplugin.internal.StagingRepositoryDescriptorRegistryBuildService
import io.github.gradlenexus.publishplugin.internal.determineStagingProfileId
import okhttp3.HttpUrl
import org.gradle.api.provider.Property
Expand All @@ -28,7 +28,8 @@ import org.gradle.api.tasks.TaskAction
abstract class InitializeNexusStagingRepository : AbstractNexusStagingRepositoryTask() {

@get:Internal
abstract val registry: Property<InvalidatingStagingRepositoryDescriptorRegistry>
// TODO use @ServiceReference instead of @Internal when minimum is Gradle 8.0.
abstract val registry: Property<StagingRepositoryDescriptorRegistryBuildService>

@get:Optional
@get:Input
Expand All @@ -44,6 +45,6 @@ abstract class InitializeNexusStagingRepository : AbstractNexusStagingRepository
val descriptor = client.createStagingRepository(stagingProfileId, repositoryDescription.get())
val consumerUrl = HttpUrl.get(serverUrl)!!.newBuilder().addEncodedPathSegments("repositories/${descriptor.stagingRepositoryId}/content/").build()
logger.lifecycle("Created staging repository '{}' at {}", descriptor.stagingRepositoryId, consumerUrl)
registry.get()[repository.name] = descriptor
registry.get().registry[repository.name] = descriptor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.github.gradlenexus.publishplugin

import io.github.gradlenexus.publishplugin.NexusRepository.PublicationType
import io.github.gradlenexus.publishplugin.internal.InvalidatingStagingRepositoryDescriptorRegistry
import io.github.gradlenexus.publishplugin.internal.StagingRepositoryDescriptorRegistryBuildService
import org.gradle.api.Action
import org.gradle.api.Plugin
Expand Down Expand Up @@ -55,8 +54,8 @@ class NexusPublishPlugin : Plugin<Project> {
"Plugin must be applied to the root project but was applied to ${project.path}"
}

require(GradleVersion.current() >= GradleVersion.version("6.0")) {
"The plugin requires Gradle version 6.0+"
require(GradleVersion.current() >= GradleVersion.version("6.2")) {
"io.github.gradle-nexus.publish-plugin requires Gradle version 6.2+"
}

val registry = createRegistry(project)
Expand All @@ -79,19 +78,18 @@ class NexusPublishPlugin : Plugin<Project> {
}
}

private fun createRegistry(rootProject: Project): Provider<InvalidatingStagingRepositoryDescriptorRegistry> {
if (GradleVersion.current() >= GradleVersion.version("6.1")) {
return rootProject.gradle.sharedServices.registerIfAbsent(
"stagingRepositoryUrlRegistry",
StagingRepositoryDescriptorRegistryBuildService::class,
Action { }
).map { it.registry }
}
val registry = InvalidatingStagingRepositoryDescriptorRegistry()
return rootProject.provider { registry }
}
private fun createRegistry(rootProject: Project): Provider<StagingRepositoryDescriptorRegistryBuildService> =
rootProject.gradle.sharedServices.registerIfAbsent(
"stagingRepositoryUrlRegistry",
StagingRepositoryDescriptorRegistryBuildService::class,
Action { }
)

private fun configureNexusTasks(rootProject: Project, extension: NexusPublishExtension, registry: Provider<InvalidatingStagingRepositoryDescriptorRegistry>) {
private fun configureNexusTasks(
rootProject: Project,
extension: NexusPublishExtension,
registryService: Provider<StagingRepositoryDescriptorRegistryBuildService>
) {
rootProject.tasks.withType(AbstractNexusStagingRepositoryTask::class.java).configureEach {
clientTimeout.convention(extension.clientTimeout)
connectTimeout.convention(extension.connectTimeout)
Expand All @@ -101,13 +99,14 @@ class NexusPublishPlugin : Plugin<Project> {
}
rootProject.tasks.withType(AbstractTransitionNexusStagingRepositoryTask::class.java).configureEach {
transitionCheckOptions.convention(extension.transitionCheckOptions)
stagingRepositoryId.convention(registry.map { it[repository.get().name].stagingRepositoryId })
usesService(registryService)
stagingRepositoryId.convention(registryService.map { it.registry[repository.get().name].stagingRepositoryId })
}
extension.repositories.all {
username.convention(rootProject.provider { rootProject.findProperty("${name}Username") as? String })
password.convention(rootProject.provider { rootProject.findProperty("${name}Password") as? String })
publicationType.convention(PublicationType.MAVEN)
configureRepositoryTasks(rootProject.tasks, extension, this, registry)
configureRepositoryTasks(rootProject.tasks, extension, this, registryService)
}
extension.repositories.whenObjectRemoved {
rootProject.tasks.named("initialize${capitalizedName}StagingRepository").configure {
Expand Down Expand Up @@ -144,7 +143,7 @@ class NexusPublishPlugin : Plugin<Project> {
tasks: TaskContainer,
extension: NexusPublishExtension,
repo: NexusRepository,
registryProvider: Provider<InvalidatingStagingRepositoryDescriptorRegistry>
registryService: Provider<StagingRepositoryDescriptorRegistryBuildService>
) {
@Suppress("UNUSED_VARIABLE") // Keep it consistent.
val retrieveStagingProfileTask = tasks.register<RetrieveStagingProfile>(
Expand All @@ -162,7 +161,8 @@ class NexusPublishPlugin : Plugin<Project> {
) {
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Initializes the staging repository in '${repo.name}' Nexus instance."
registry.set(registryProvider)
registry.set(registryService)
usesService(registryService)
repository.convention(repo)
packageGroup.convention(extension.packageGroup)
}
Expand All @@ -171,7 +171,8 @@ class NexusPublishPlugin : Plugin<Project> {
) {
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Finds the staging repository for ${repo.name}"
registry.set(registryProvider)
registry.set(registryService)
usesService(registryService)
repository.convention(repo)
packageGroup.convention(extension.packageGroup)
descriptionRegex.convention(extension.repositoryDescription.map { "\\b" + Regex.escape(it) + "(\\s|$)" })
Expand Down Expand Up @@ -211,7 +212,11 @@ class NexusPublishPlugin : Plugin<Project> {
}
}

private fun configurePublishingForAllProjects(rootProject: Project, extension: NexusPublishExtension, registry: Provider<InvalidatingStagingRepositoryDescriptorRegistry>) {
private fun configurePublishingForAllProjects(
rootProject: Project,
extension: NexusPublishExtension,
registry: Provider<StagingRepositoryDescriptorRegistryBuildService>
) {
rootProject.afterEvaluate {
allprojects {
val publishingProject = this
Expand Down Expand Up @@ -250,7 +255,7 @@ class NexusPublishPlugin : Plugin<Project> {
private fun addPublicationRepositories(
project: Project,
extension: NexusPublishExtension,
registry: Provider<InvalidatingStagingRepositoryDescriptorRegistry>
registry: Provider<StagingRepositoryDescriptorRegistryBuildService>
): Map<NexusRepository, ArtifactRepository> = extension.repositories.associateWith { nexusRepo ->
createArtifactRepository(nexusRepo.publicationType.get(), project, nexusRepo, extension, registry)
}
Expand All @@ -260,7 +265,7 @@ class NexusPublishPlugin : Plugin<Project> {
project: Project,
nexusRepo: NexusRepository,
extension: NexusPublishExtension,
registry: Provider<InvalidatingStagingRepositoryDescriptorRegistry>
registry: Provider<StagingRepositoryDescriptorRegistryBuildService>
): ArtifactRepository = when (publicationType) {
PublicationType.MAVEN -> project.theExtension<PublishingExtension>().repositories.maven {
configureArtifactRepo(nexusRepo, extension, registry, false)
Expand All @@ -279,7 +284,7 @@ class NexusPublishPlugin : Plugin<Project> {
private fun <T> T.configureArtifactRepo(
nexusRepo: NexusRepository,
extension: NexusPublishExtension,
registry: Provider<InvalidatingStagingRepositoryDescriptorRegistry>,
registry: Provider<StagingRepositoryDescriptorRegistryBuildService>,
provideFallback: Boolean
) where T : UrlArtifactRepository, T : ArtifactRepository, T : AuthenticationSupported {
name = nexusRepo.name
Expand Down Expand Up @@ -335,13 +340,13 @@ class NexusPublishPlugin : Plugin<Project> {
private fun getRepoUrl(
nexusRepo: NexusRepository,
extension: NexusPublishExtension,
registry: Provider<InvalidatingStagingRepositoryDescriptorRegistry>,
registry: Provider<StagingRepositoryDescriptorRegistryBuildService>,
provideFallback: Boolean,
artifactRepo: ArtifactRepository
): Provider<URI> =
extension.useStaging.flatMap { useStaging ->
if (useStaging) {
registry.map { descriptorRegistry ->
registry.map { it.registry }.map { descriptorRegistry ->
if (provideFallback) {
descriptorRegistry.invalidateLater(nexusRepo.name, artifactRepo)
descriptorRegistry.tryGet(nexusRepo.name)?.stagingRepositoryUrl ?: nexusRepo.nexusUrl.get()
Expand Down Expand Up @@ -395,3 +400,17 @@ private inline fun <reified T : Any> Project.theExtension(): T =
this.extensions.findByType(it)
?: error("The plugin cannot be applied without the publishing plugin")
}

private fun <T> Provider<T>.forUseAtConfigurationTimeCompat(): Provider<T> =
if (GradleVersion.current() < GradleVersion.version("6.5")) {
// Gradle < 6.5 doesn't have this function.
this
} else if (GradleVersion.current() < GradleVersion.version("7.4")) {
// Gradle 6.5 - 7.3 requires this function to be called.
@Suppress("DEPRECATION")
this.forUseAtConfigurationTime()
} else {
// Gradle >= 7.4 deprecated this function in favor of not calling it (became no-op, and will eventually nag).
// https://docs.gradle.org/current/userguide/upgrading_version_7.html#for_use_at_configuration_time_deprecation
this
}
4 changes: 2 additions & 2 deletions stutter.lockfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DO NOT MODIFY: Generated by Stutter plugin.
java11=6.0.1,6.9.4,7.0.2,7.6.2,8.0.2,8.2.1
java11=6.2.2,6.9.4,7.0.2,7.6.2,8.0.2,8.2.1
java17=7.3.3,7.6.2,8.0.2,8.2.1
java8=6.0.1,6.9.4,7.0.2,7.6.2,8.0.2,8.2.1
java8=6.2.2,6.9.4,7.0.2,7.6.2,8.0.2,8.2.1

0 comments on commit ecfc17e

Please sign in to comment.