Skip to content

Commit 15d2f87

Browse files
committed
Add Gradle task to complete MavenCentral deployment automatically
1 parent f154876 commit 15d2f87

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

build.gradle

Lines changed: 40 additions & 1 deletion
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!
@@ -331,3 +331,42 @@ if (!Boolean.parseBoolean(System.getenv('CI')) || project.hasProperty('signingKe
331331
sign publishing.publications.mavenJava
332332
}
333333
}
334+
335+
tasks.register('completeMavenCentralDeployment') {
336+
group = PublishingPlugin.PUBLISH_TASK_GROUP
337+
description = 'Completes the MavenCentral deployment after running normal publishing'
338+
339+
String bearerToken = 'NOT_SET'
340+
if (project.hasProperty('SB_SONATYPE_USER') && project.hasProperty('SB_SONATYPE_PASSWORD')) {
341+
bearerToken = project.getProperty('SB_SONATYPE_USER') + ':' + project.getProperty('SB_SONATYPE_PASSWORD')
342+
bearerToken = Base64.encoder.encodeToString(bearerToken.getBytes('UTF-8'))
343+
} else {
344+
throw new GradleException('Sonatype credentials not set, missing SB_SONATYPE_USER and/or SB_SONATYPE_PASSWORD')
345+
}
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').configure {
371+
it.finalizedBy(tasks.named('completeMavenCentralDeployment'))
372+
}

0 commit comments

Comments
 (0)