diff --git a/build-plugins/build-support/build.gradle.kts b/build-plugins/build-support/build.gradle.kts index f1ee43a..7780222 100644 --- a/build-plugins/build-support/build.gradle.kts +++ b/build-plugins/build-support/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { } implementation(libs.jReleaserPlugin) + implementation(libs.nexusPublishPlugin) compileOnly(gradleApi()) implementation(libs.aws.sdk.s3) implementation(libs.aws.sdk.cloudwatch) diff --git a/build-plugins/build-support/src/main/kotlin/aws/sdk/kotlin/gradle/dsl/Publish.kt b/build-plugins/build-support/src/main/kotlin/aws/sdk/kotlin/gradle/dsl/Publish.kt index df34813..862a465 100644 --- a/build-plugins/build-support/src/main/kotlin/aws/sdk/kotlin/gradle/dsl/Publish.kt +++ b/build-plugins/build-support/src/main/kotlin/aws/sdk/kotlin/gradle/dsl/Publish.kt @@ -5,6 +5,7 @@ package aws.sdk.kotlin.gradle.dsl import aws.sdk.kotlin.gradle.util.verifyRootProject +import io.github.gradlenexus.publishplugin.NexusPublishExtension import org.gradle.api.Project import org.gradle.api.publish.PublishingExtension import org.gradle.api.publish.maven.MavenPublication @@ -15,9 +16,17 @@ import org.gradle.plugins.signing.Sign import org.gradle.plugins.signing.SigningExtension import org.jreleaser.gradle.plugin.JReleaserExtension import org.jreleaser.model.Active +import java.time.Duration private object Properties { const val SKIP_PUBLISHING = "skipPublish" + + // Nexus publish plugin properties + const val PUBLISH_GROUP_NAME_PROP = "publishGroupName" + const val SIGNING_KEY_PROP = "signingKey" + const val SIGNING_PASSWORD_PROP = "signingPassword" + const val SONATYPE_USERNAME_PROP = "sonatypeUsername" + const val SONATYPE_PASSWORD_PROP = "sonatypePassword" } private object EnvironmentVariables { @@ -61,7 +70,11 @@ fun Project.skipPublishing() { * @param repoName the repository name (e.g. `smithy-kotlin`, `aws-sdk-kotlin`, etc) * @param githubOrganization the name of the GitHub organization that [repoName] is located in */ -fun Project.configurePublishing(repoName: String, githubOrganization: String = "awslabs") { +fun Project.configurePublishing( + repoName: String, + githubOrganization: String = "awslabs", + useNexusPublishPlugin: Boolean = true, +) { val project = this apply(plugin = "maven-publish") @@ -111,8 +124,17 @@ fun Project.configurePublishing(repoName: String, githubOrganization: String = " } } - val secretKey = System.getenv(EnvironmentVariables.GPG_SECRET_KEY) - val passphrase = System.getenv(EnvironmentVariables.GPG_PASSPHRASE) + val secretKey = if (useNexusPublishPlugin) { + project.property(Properties.SIGNING_KEY_PROP) as String + } else { + System.getenv(EnvironmentVariables.GPG_SECRET_KEY) + } + + val passphrase = if (useNexusPublishPlugin) { + project.property(Properties.SIGNING_PASSWORD_PROP) as String + } else { + System.getenv(EnvironmentVariables.GPG_PASSPHRASE) + } if (!secretKey.isNullOrBlank() && !passphrase.isNullOrBlank()) { apply(plugin = "signing") @@ -218,6 +240,46 @@ fun Project.configureJReleaser() { } } +/** + * Configure nexus publishing plugin. This (conditionally) enables the `gradle-nexus.publish-plugin` and configures it. + */ +fun Project.configureNexus( + nexusUrl: String = "https://ossrh-staging-api.central.sonatype.com/service/local/", + snapshotRepositoryUrl: String = "https://central.sonatype.com/repository/maven-snapshots/", +) { + verifyRootProject { "Kotlin SDK nexus configuration must be applied to the root project only" } + + val requiredProps = listOf( + Properties.SONATYPE_USERNAME_PROP, + Properties.SONATYPE_PASSWORD_PROP, + Properties.PUBLISH_GROUP_NAME_PROP, + ) + val doConfigure = requiredProps.all { project.hasProperty(it) } + if (!doConfigure) { + logger.info("skipping nexus configuration, missing one or more required properties: $requiredProps") + return + } + + apply(plugin = "io.github.gradle-nexus.publish-plugin") + extensions.configure { + val publishGroupName = project.property(Properties.PUBLISH_GROUP_NAME_PROP) as String + group = publishGroupName + packageGroup.set(publishGroupName) + repositories { + create("awsNexus") { + this.nexusUrl.set(uri(nexusUrl)) + this.snapshotRepositoryUrl.set(uri(snapshotRepositoryUrl)) + username.set(project.property(Properties.SONATYPE_USERNAME_PROP) as String) + password.set(project.property(Properties.SONATYPE_PASSWORD_PROP) as String) + } + } + + transitionCheckOptions { + maxRetries.set(180) + delayBetween.set(Duration.ofSeconds(10)) + } + } +} private fun isAvailableForPublication(project: Project, publication: MavenPublication): Boolean { var shouldPublish = true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index da7c486..a8ac1b8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,6 +3,7 @@ aws-sdk-version = "1.4.116" kotlin-version = "2.2.0" ktlint = "1.3.0" jreleaser-plugin-version = "1.18.0" +nexus-plugin-version = "2.0.0" publish-plugin-version = "1.3.1" smithy-version = "1.60.2" smithy-gradle-plugin-version = "1.3.0" @@ -15,6 +16,7 @@ ktlint-cli = { module = "com.pinterest.ktlint:ktlint-cli", version.ref = "ktlint ktlint-cli-ruleset-core = { module = "com.pinterest.ktlint:ktlint-cli-ruleset-core", version.ref = "ktlint" } ktlint-test = {module = "com.pinterest.ktlint:ktlint-test", version.ref = "ktlint" } jReleaserPlugin = { module = "org.jreleaser:jreleaser-gradle-plugin", version.ref = "jreleaser-plugin-version" } +nexusPublishPlugin = { module = "io.github.gradle-nexus:publish-plugin", version.ref = "nexus-plugin-version" } smithy-model = { module = "software.amazon.smithy:smithy-model", version.ref = "smithy-version" } smithy-gradle-base-plugin = { module = "software.amazon.smithy.gradle:smithy-base", version.ref = "smithy-gradle-plugin-version" }