1515 */
1616/*
1717 * A note about publishing and signing.
18- * Maven central requires that artifacts be signed. And upload is done to Sonatype .
18+ * Maven central requires that artifacts be signed. And upload is done via Maven Central Portal .
1919 * To publish you will need these environment variables defined:
20- * SONATYPE_TOKEN_USERNAME
21- * SONATYPE_TOKEN_PASSWORD
20+ * MAVEN_CENTRAL_PORTAL_TOKEN_USERNAME
21+ * MAVEN_CENTRAL_PORTAL_TOKEN_PASSWORD
2222 * MAVEN_GPG_PRIVATE_KEY
2323 * MAVEN_GPG_PASSPHRASE
24- * Suggestion is to put these in a shell script with restricted read permissions, then source it before calling
25- * ./gradlew publish.
24+ * If run manually, the suggestion is to put these in a shell script with restricted read permissions,
25+ * then source it before calling ./gradlew
26+ * If run from a Github action, make sure these environment variables are defined.
2627 */
2728plugins {
2829 id ' java'
@@ -58,6 +59,7 @@ allprojects {
5859
5960 tasks. withType(Javadoc ) {
6061 options. encoding = ' UTF-8'
62+ options. addStringOption(' Xdoclint:none' , ' -quiet' )
6163 }
6264
6365 // All projects that include the 'java` plugin will have a Test task by default.
@@ -82,16 +84,6 @@ subprojects {
8284 apply plugin : ' java'
8385 apply plugin : libs. plugins. spotless. get(). pluginId
8486
85- // Cannot publish a SNAPSHOT. The provided sonatype url will not accept it.
86- tasks. withType(PublishToMavenRepository ). all { task ->
87- task. onlyIf {
88- if (project. version. toString(). contains(' SNAPSHOT' )) {
89- throw new GradleException (" Publishing is not allowed for SNAPSHOT versions. Currently " + project. version)
90- }
91- true
92- }
93- }
94-
9587 task javadocJar(type : Jar ) {
9688 archiveClassifier. set(' javadoc' )
9789 from javadoc
@@ -111,24 +103,29 @@ subprojects {
111103
112104 // These modules require the same publishing configuration, apart from the name of the module
113105 // Also we want to limit artefact publishing to these modules.
114- if (project. name == ' main' ||
115- project. name == ' core' ||
116- project. name == ' model' ) {
106+ if (project. name == ' main'
107+ || project. name == ' core'
108+ || project. name == ' model'
109+ ) {
117110 def fullProjectName = ' gtfs-validator-' + project. name
118111
119112 afterEvaluate {
120113 publishing {
121114 repositories {
122- // This is the sonatype staging repo for maven.
123- // Once uploaded, the repo needs to be manually closed, which will trigger acceptance tests for
124- // maven central (but not transfer yet).
125- // Once successfully closed, the repo is available for testing.
126- // After testing, it can be manually promoted on the sonatype site, which will then publish to maven central.
127115 maven {
128- url = ' https://s01.oss.sonatype.org/service/local/staging/deploy/maven2'
129- credentials {
130- username System . getenv(" SONATYPE_TOKEN_USERNAME" )
131- password System . getenv(" SONATYPE_TOKEN_PASSWORD" )
116+ // Snapshot uses a different repository than release.
117+ // The publish in that case will upload to Maven Central snapshot repository.
118+ if (version. endsWith(' SNAPSHOT' )) {
119+ url = ' https://central.sonatype.com/repository/maven-snapshots/'
120+ credentials {
121+ username = System . getenv(" MAVEN_CENTRAL_PORTAL_TOKEN_USERNAME" )
122+ password = System . getenv(" MAVEN_CENTRAL_PORTAL_TOKEN_PASSWORD" )
123+ }
124+ } else {
125+ // For releases, the publish task does not upload to MavenCentral.
126+ // It just the artifacts directory hierarchy on the local disk,
127+ // The actual zipping and uploading is done in the publishToMavenCentral task.
128+ url = " ${ buildDir} /local-repo"
132129 }
133130 }
134131 }
@@ -172,9 +169,51 @@ subprojects {
172169 useInMemoryPgpKeys(System . getenv(' MAVEN_GPG_PRIVATE_KEY' ), System . getenv(' MAVEN_GPG_PASSPHRASE' ))
173170 sign publishing. publications. mavenJava
174171 }
172+
173+
174+ tasks. register(' publishToMavenCentral' ) {
175+ // We want to do call the publish task only if the version is a release (ie not a snapshot).
176+ // Without this conditional, with a snapshot it would do the publish first, then throw the Exception below.
177+ // But since the dependsOn is within the conditional, the dependency is not created for snapshot versions
178+ // so the publish in that case is never executed.
179+ if (! version. toString(). endsWith(' SNAPSHOT' )) {
180+ dependsOn tasks. publish
181+ }
182+ doFirst {
183+ if (version. toString(). endsWith(' SNAPSHOT' )) {
184+ throw new GradleException (" Version \" " + version +
185+ " \" is a snapshot. Cannot publish to Maven Central" )
186+ }
187+ }
188+
189+ doLast {
190+ exec {
191+ workingDir project. projectDir
192+ commandLine ' bash' , " ${ project.rootDir} /scripts/publish_to_maven_central.sh" , project. name, project. version
193+ }
194+ }
195+ }
196+
197+ tasks. register(' publishToSnapshotRepository' ) {
198+ // We want to do call the publish task only if the version is a snapshot.
199+ // Without this conditional, with a release it would do the publish first, then throw the Exception below.
200+ // But since the dependsOn is within the conditional, the dependency is not created for release versions
201+ // so the publish in that case is never executed.
202+ if (version. toString(). endsWith(' SNAPSHOT' )) {
203+ dependsOn tasks. publish
204+ }
205+ doFirst {
206+ if (! version. toString(). endsWith(' SNAPSHOT' )) {
207+ throw new GradleException (" Version \" " + version +
208+ " \" is a NOT a snapshot. Cannot publish to the snapshot repository" )
209+ }
210+ }
211+ }
175212 }
176213
214+
177215 }
216+
178217 compileJava {
179218 options. compilerArgs << ' -parameters'
180219 }
0 commit comments