diff --git a/build.gradle b/build.gradle index 7394ce4..379b78f 100644 --- a/build.gradle +++ b/build.gradle @@ -171,6 +171,56 @@ subprojects { required { signingRequired } sign publishing.publications.mavenJava } + + // see https://github.com/conductor-oss/conductor/blob/main/deploy.gradle#L76-L129 + task promoteToMavenCentral { + group = 'publishing' + description = 'Promotes staged artifacts to Maven Central' + + onlyIf { + sonatypeCredentialsAvailable && !isSnapshot + } + + doLast { + // Create base64 encoded token for authentication + def token = "${sonatypeUsername}:${sonatypePassword}".bytes.encodeBase64().toString() + + // Get open staging repositories + def response = new URL("https://ossrh-staging-api.central.sonatype.com/manual/search/repositories") + .openConnection() + response.setRequestProperty("Authorization", "Basic ${token}") + response.setRequestProperty("Content-Type", "application/json") + + def repositories = new groovy.json.JsonSlurper().parse(response.inputStream) + + // Promote each open repository + repositories.repositories.each { repo -> + if (repo.state == "open") { + project.logger.lifecycle("Promoting repository ${repo.key}") + + def promoteUrl = new URL("https://ossrh-staging-api.central.sonatype.com/manual/upload/repository/${repo.key}?publishing_type=automatic") + def connection = promoteUrl.openConnection() + connection.setRequestMethod("POST") + connection.setRequestProperty("Authorization", "Basic ${token}") + connection.setRequestProperty("Content-Type", "application/json") + + def responseCode = connection.responseCode + if (responseCode == 200) { + project.logger.lifecycle("Successfully promoted repository ${repo.key}") + } else { + def errorStream = connection.errorStream + def errorBody = errorStream ? errorStream.text : "No error body available" + def errorMessage = "Failed to promote repository ${repo.key}. Response code: ${responseCode}. Response message: ${connection.responseMessage}. Error body: ${errorBody}" + project.logger.error(errorMessage) + //throw new GradleException(errorMessage) + } + } + } + } + } + tasks.matching { it.name == 'publish' }.configureEach { publishTask -> + publishTask.finalizedBy tasks.promoteToMavenCentral + } } def getGitCommit() {