55package aws.sdk.kotlin.gradle.dsl
66
77import aws.sdk.kotlin.gradle.util.verifyRootProject
8+ import io.github.gradlenexus.publishplugin.NexusPublishExtension
89import org.gradle.api.Project
910import org.gradle.api.publish.PublishingExtension
1011import org.gradle.api.publish.maven.MavenPublication
@@ -15,11 +16,19 @@ import org.gradle.plugins.signing.Sign
1516import org.gradle.plugins.signing.SigningExtension
1617import org.jreleaser.gradle.plugin.JReleaserExtension
1718import org.jreleaser.model.Active
19+ import java.time.Duration
1820
1921private object Properties {
2022 const val SKIP_PUBLISHING = " skipPublish"
2123}
2224
25+ // TODO Remove once aws-sdk-kotlin migrates to Central Portal
26+ private const val PUBLISH_GROUP_NAME_PROP = " publishGroupName"
27+ private const val SIGNING_KEY_PROP = " signingKey"
28+ private const val SIGNING_PASSWORD_PROP = " signingPassword"
29+ private const val SONATYPE_USERNAME_PROP = " sonatypeUsername"
30+ private const val SONATYPE_PASSWORD_PROP = " sonatypePassword"
31+
2332private object EnvironmentVariables {
2433 const val GROUP_ID = " JRELEASER_PROJECT_JAVA_GROUP_ID"
2534 const val MAVEN_CENTRAL_USERNAME = " JRELEASER_MAVENCENTRAL_USERNAME"
@@ -61,6 +70,102 @@ fun Project.skipPublishing() {
6170 extra.set(Properties .SKIP_PUBLISHING , true )
6271}
6372
73+ // TODO Remove this once aws-sdk-kotlin migrates to Central Portal
74+ fun Project.configureNexusPublishing (repoName : String , githubOrganization : String = "awslabs") {
75+ val project = this
76+ apply (plugin = " maven-publish" )
77+
78+ // FIXME: create a real "javadoc" JAR from Dokka output
79+ val javadocJar = tasks.register<Jar >(" emptyJar" ) {
80+ archiveClassifier.set(" javadoc" )
81+ destinationDirectory.set(layout.buildDirectory.dir(" libs" ))
82+ from()
83+ }
84+
85+ extensions.configure<PublishingExtension > {
86+ repositories {
87+ maven {
88+ name = " testLocal"
89+ url = rootProject.layout.buildDirectory.dir(" m2" ).get().asFile.toURI()
90+ }
91+ }
92+
93+ publications.all {
94+ if (this !is MavenPublication ) return @all
95+
96+ project.afterEvaluate {
97+ pom {
98+ name.set(project.name)
99+ description.set(project.description)
100+ url.set(" https://github.com/$githubOrganization /$repoName " )
101+ licenses {
102+ license {
103+ name.set(" Apache-2.0" )
104+ url.set(" https://www.apache.org/licenses/LICENSE-2.0.txt" )
105+ }
106+ }
107+ developers {
108+ developer {
109+ id.set(repoName)
110+ name.set(" AWS SDK Kotlin Team" )
111+ }
112+ }
113+ scm {
114+ connection.set(" scm:git:git://github.com/$githubOrganization /$repoName .git" )
115+ developerConnection.set(" scm:git:ssh://github.com/$githubOrganization /$repoName .git" )
116+ url.set(" https://github.com/$githubOrganization /$repoName " )
117+ }
118+
119+ artifact(javadocJar)
120+ }
121+ }
122+ }
123+
124+ if (project.hasProperty(SIGNING_KEY_PROP ) && project.hasProperty(SIGNING_PASSWORD_PROP )) {
125+ apply (plugin = " signing" )
126+ extensions.configure<SigningExtension > {
127+ useInMemoryPgpKeys(
128+ project.property(SIGNING_KEY_PROP ) as String ,
129+ project.property(SIGNING_PASSWORD_PROP ) as String ,
130+ )
131+ sign(publications)
132+ }
133+
134+ // FIXME - workaround for https://github.com/gradle/gradle/issues/26091
135+ val signingTasks = tasks.withType<Sign >()
136+ tasks.withType<AbstractPublishToMaven >().configureEach {
137+ mustRunAfter(signingTasks)
138+ }
139+ }
140+ }
141+
142+ fun isAvailableForNexusPublication (project : Project , publication : MavenPublication ): Boolean {
143+ var shouldPublish = true
144+
145+ // Check SKIP_PUBLISH_PROP
146+ if (project.extra.has(Properties .SKIP_PUBLISHING )) shouldPublish = false
147+
148+ // Only publish publications with the configured group from JReleaser or everything if JReleaser group is not configured
149+ val publishGroupName = project.findProperty(PUBLISH_GROUP_NAME_PROP ) as ? String
150+ shouldPublish = shouldPublish && (publishGroupName == null || publication.groupId.startsWith(publishGroupName))
151+
152+ // Validate publication name is allowed to be published
153+ shouldPublish = shouldPublish && ALLOWED_PUBLICATION_NAMES .any { publication.name.equals(it, ignoreCase = true ) }
154+
155+ return shouldPublish
156+ }
157+
158+ tasks.withType<AbstractPublishToMaven >().configureEach {
159+ onlyIf {
160+ isAvailableForNexusPublication(project, publication).also {
161+ if (! it) {
162+ logger.warn(" Skipping publication, project=${project.name} ; publication=${publication.name} ; group=${publication.groupId} " )
163+ }
164+ }
165+ }
166+ }
167+ }
168+
64169/* *
65170 * Configure publishing for this project. This applies the `maven-publish` and `signing` plugins and configures
66171 * the publications.
@@ -149,6 +254,43 @@ fun Project.configurePublishing(repoName: String, githubOrganization: String = "
149254 }
150255}
151256
257+ /* *
258+ * Configure nexus publishing plugin. This (conditionally) enables the `gradle-nexus.publish-plugin` and configures it.
259+ */
260+ fun Project.configureNexus (
261+ nexusUrl : String = "https : // ossrh-staging-api.central.sonatype.com/service/local/",
262+ snapshotRepositoryUrl : String = "https : // central.sonatype.com/repository/maven-snapshots/",
263+ ) {
264+ verifyRootProject { " Kotlin SDK nexus configuration must be applied to the root project only" }
265+
266+ val requiredProps = listOf (SONATYPE_USERNAME_PROP , SONATYPE_PASSWORD_PROP , PUBLISH_GROUP_NAME_PROP )
267+ val doConfigure = requiredProps.all { project.hasProperty(it) }
268+ if (! doConfigure) {
269+ logger.info(" skipping nexus configuration, missing one or more required properties: $requiredProps " )
270+ return
271+ }
272+
273+ apply (plugin = " io.github.gradle-nexus.publish-plugin" )
274+ extensions.configure<NexusPublishExtension > {
275+ val publishGroupName = project.property(PUBLISH_GROUP_NAME_PROP ) as String
276+ group = publishGroupName
277+ packageGroup.set(publishGroupName)
278+ repositories {
279+ create(" awsNexus" ) {
280+ this .nexusUrl.set(uri(nexusUrl))
281+ this .snapshotRepositoryUrl.set(uri(snapshotRepositoryUrl))
282+ username.set(project.property(SONATYPE_USERNAME_PROP ) as String )
283+ password.set(project.property(SONATYPE_PASSWORD_PROP ) as String )
284+ }
285+ }
286+
287+ transitionCheckOptions {
288+ maxRetries.set(180 )
289+ delayBetween.set(Duration .ofSeconds(10 ))
290+ }
291+ }
292+ }
293+
152294/* *
153295 * Configure JReleaser publishing plugin. This (conditionally) enables the `org.jreleaser` plugin and configures it.
154296 */
0 commit comments