Skip to content

Commit 2f8cad4

Browse files
committed
Add Gradle task to complete MavenCentral deployment automatically
1 parent f154876 commit 2f8cad4

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

build.gradle

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ repositories {
1818
mavenCentral()
1919
}
2020

21-
2221
import org.gradle.internal.jvm.Jvm
2322

2423
/*
@@ -44,6 +43,7 @@ ext {
4443
publishedName = 'standard-bom'
4544
website = 'https://sbom.siemens.io/'
4645
codeRepo = 'https://github.com/siemens/standard-bom-java'
46+
mavenCentralNamespace = 'com.siemens.sbom'
4747

4848
// The version of the spec we are currently implementing.
4949
// Must correspond to an existing tag, without the leading 'v', or validation may fail later!
@@ -268,13 +268,17 @@ dependencies {
268268
/*
269269
* - - - - PUBLISHING - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
270270
*/
271+
def hasSonatypeCredentials() {
272+
return project.hasProperty('SB_SONATYPE_USER') && project.hasProperty('SB_SONATYPE_PASSWORD')
273+
}
274+
271275
publishing {
272276
repositories {
273277
maven {
274278
name = 'localBuildDir'
275279
url = project.layout.buildDirectory.dir('maven-repo-test')
276280
}
277-
if (project.hasProperty('SB_SONATYPE_USER') && project.hasProperty('SB_SONATYPE_PASSWORD')) {
281+
if (hasSonatypeCredentials()) {
278282
maven {
279283
name = 'mavenCentralStaging'
280284
url = 'https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/'
@@ -331,3 +335,39 @@ if (!Boolean.parseBoolean(System.getenv('CI')) || project.hasProperty('signingKe
331335
sign publishing.publications.mavenJava
332336
}
333337
}
338+
339+
if (hasSonatypeCredentials()) {
340+
tasks.register('completeMavenCentralDeployment') {
341+
group = PublishingPlugin.PUBLISH_TASK_GROUP
342+
description = 'Completes the MavenCentral deployment after running normal publishing'
343+
344+
String bearerToken = project.getProperty('SB_SONATYPE_USER') + ':' + project.getProperty('SB_SONATYPE_PASSWORD')
345+
bearerToken = Base64.encoder.encodeToString(bearerToken.getBytes('UTF-8'))
346+
347+
doLast {
348+
def connection = new URL('https://ossrh-staging-api.central.sonatype.com/manual/upload/defaultRepository/'
349+
+ project.ext.mavenCentralNamespace + '?publishing_type=automatic').openConnection() as HttpURLConnection
350+
try {
351+
connection.with {
352+
requestMethod = 'POST'
353+
doOutput = false
354+
setRequestProperty('Authorization', "Bearer ${bearerToken}")
355+
356+
int responseCode = getResponseCode()
357+
if (responseCode == 200) {
358+
logger.lifecycle('MavenCentral deployment completed. '
359+
+ 'Details at https://central.sonatype.com/publishing/deployments')
360+
} else {
361+
throw new GradleException("Request failed with status ${responseCode}")
362+
}
363+
}
364+
} finally {
365+
connection.disconnect()
366+
}
367+
}
368+
}
369+
370+
tasks.named('publishAllPublicationsToMavenCentralStagingRepository') {
371+
it.finalizedBy(tasks.named('completeMavenCentralDeployment'))
372+
}
373+
}

0 commit comments

Comments
 (0)