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"
@@ -55,6 +64,102 @@ fun Project.skipPublishing() {
5564 extra.set(Properties .SKIP_PUBLISHING , true )
5665}
5766
67+ // TODO Remove this once aws-sdk-kotlin migrates to Central Portal
68+ fun Project.configureNexusPublishing (repoName : String , githubOrganization : String = "awslabs") {
69+ val project = this
70+ apply (plugin = " maven-publish" )
71+
72+ // FIXME: create a real "javadoc" JAR from Dokka output
73+ val javadocJar = tasks.register<Jar >(" emptyJar" ) {
74+ archiveClassifier.set(" javadoc" )
75+ destinationDirectory.set(layout.buildDirectory.dir(" libs" ))
76+ from()
77+ }
78+
79+ extensions.configure<PublishingExtension > {
80+ repositories {
81+ maven {
82+ name = " testLocal"
83+ url = rootProject.layout.buildDirectory.dir(" m2" ).get().asFile.toURI()
84+ }
85+ }
86+
87+ publications.all {
88+ if (this !is MavenPublication ) return @all
89+
90+ project.afterEvaluate {
91+ pom {
92+ name.set(project.name)
93+ description.set(project.description)
94+ url.set(" https://github.com/$githubOrganization /$repoName " )
95+ licenses {
96+ license {
97+ name.set(" Apache-2.0" )
98+ url.set(" https://www.apache.org/licenses/LICENSE-2.0.txt" )
99+ }
100+ }
101+ developers {
102+ developer {
103+ id.set(repoName)
104+ name.set(" AWS SDK Kotlin Team" )
105+ }
106+ }
107+ scm {
108+ connection.set(" scm:git:git://github.com/$githubOrganization /$repoName .git" )
109+ developerConnection.set(" scm:git:ssh://github.com/$githubOrganization /$repoName .git" )
110+ url.set(" https://github.com/$githubOrganization /$repoName " )
111+ }
112+
113+ artifact(javadocJar)
114+ }
115+ }
116+ }
117+
118+ if (project.hasProperty(SIGNING_KEY_PROP ) && project.hasProperty(SIGNING_PASSWORD_PROP )) {
119+ apply (plugin = " signing" )
120+ extensions.configure<SigningExtension > {
121+ useInMemoryPgpKeys(
122+ project.property(SIGNING_KEY_PROP ) as String ,
123+ project.property(SIGNING_PASSWORD_PROP ) as String ,
124+ )
125+ sign(publications)
126+ }
127+
128+ // FIXME - workaround for https://github.com/gradle/gradle/issues/26091
129+ val signingTasks = tasks.withType<Sign >()
130+ tasks.withType<AbstractPublishToMaven >().configureEach {
131+ mustRunAfter(signingTasks)
132+ }
133+ }
134+ }
135+
136+ fun isAvailableForNexusPublication (project : Project , publication : MavenPublication ): Boolean {
137+ var shouldPublish = true
138+
139+ // Check SKIP_PUBLISH_PROP
140+ if (project.extra.has(Properties .SKIP_PUBLISHING )) shouldPublish = false
141+
142+ // Only publish publications with the configured group from JReleaser or everything if JReleaser group is not configured
143+ val publishGroupName = project.findProperty(PUBLISH_GROUP_NAME_PROP ) as ? String
144+ shouldPublish = shouldPublish && (publishGroupName == null || publication.groupId.startsWith(publishGroupName))
145+
146+ // Validate publication name is allowed to be published
147+ shouldPublish = shouldPublish && ALLOWED_PUBLICATION_NAMES .any { publication.name.equals(it, ignoreCase = true ) }
148+
149+ return shouldPublish
150+ }
151+
152+ tasks.withType<AbstractPublishToMaven >().configureEach {
153+ onlyIf {
154+ isAvailableForNexusPublication(project, publication).also {
155+ if (! it) {
156+ logger.warn(" Skipping publication, project=${project.name} ; publication=${publication.name} ; group=${publication.groupId} " )
157+ }
158+ }
159+ }
160+ }
161+ }
162+
58163/* *
59164 * Configure publishing for this project. This applies the `maven-publish` and `signing` plugins and configures
60165 * the publications.
@@ -143,6 +248,43 @@ fun Project.configurePublishing(repoName: String, githubOrganization: String = "
143248 }
144249}
145250
251+ /* *
252+ * Configure nexus publishing plugin. This (conditionally) enables the `gradle-nexus.publish-plugin` and configures it.
253+ */
254+ fun Project.configureNexus (
255+ nexusUrl : String = "https : // ossrh-staging-api.central.sonatype.com/service/local/",
256+ snapshotRepositoryUrl : String = "https : // central.sonatype.com/repository/maven-snapshots/",
257+ ) {
258+ verifyRootProject { " Kotlin SDK nexus configuration must be applied to the root project only" }
259+
260+ val requiredProps = listOf (SONATYPE_USERNAME_PROP , SONATYPE_PASSWORD_PROP , PUBLISH_GROUP_NAME_PROP )
261+ val doConfigure = requiredProps.all { project.hasProperty(it) }
262+ if (! doConfigure) {
263+ logger.info(" skipping nexus configuration, missing one or more required properties: $requiredProps " )
264+ return
265+ }
266+
267+ apply (plugin = " io.github.gradle-nexus.publish-plugin" )
268+ extensions.configure<NexusPublishExtension > {
269+ val publishGroupName = project.property(PUBLISH_GROUP_NAME_PROP ) as String
270+ group = publishGroupName
271+ packageGroup.set(publishGroupName)
272+ repositories {
273+ create(" awsNexus" ) {
274+ this .nexusUrl.set(uri(nexusUrl))
275+ this .snapshotRepositoryUrl.set(uri(snapshotRepositoryUrl))
276+ username.set(project.property(SONATYPE_USERNAME_PROP ) as String )
277+ password.set(project.property(SONATYPE_PASSWORD_PROP ) as String )
278+ }
279+ }
280+
281+ transitionCheckOptions {
282+ maxRetries.set(180 )
283+ delayBetween.set(Duration .ofSeconds(10 ))
284+ }
285+ }
286+ }
287+
146288/* *
147289 * Configure JReleaser publishing plugin. This (conditionally) enables the `org.jreleaser` plugin and configures it.
148290 */
0 commit comments