@@ -190,6 +190,56 @@ subprojects {
190190 required { signingRequired }
191191 sign publishing. publications. mavenJava
192192 }
193+
194+ // see https://github.com/conductor-oss/conductor/blob/main/deploy.gradle#L76-L129
195+ task promoteToMavenCentral {
196+ group = ' publishing'
197+ description = ' Promotes staged artifacts to Maven Central'
198+
199+ onlyIf {
200+ sonatypeCredentialsAvailable && ! isSnapshot
201+ }
202+
203+ doLast {
204+ // Create base64 encoded token for authentication
205+ def token = " ${ sonatypeUsername} :${ sonatypePassword} " . bytes. encodeBase64(). toString()
206+
207+ // Get open staging repositories
208+ def response = new URL (" https://ossrh-staging-api.central.sonatype.com/manual/search/repositories" )
209+ .openConnection()
210+ response. setRequestProperty(" Authorization" , " Basic ${ token} " )
211+ response. setRequestProperty(" Content-Type" , " application/json" )
212+
213+ def repositories = new groovy.json.JsonSlurper (). parse(response. inputStream)
214+
215+ // Promote each open repository
216+ repositories. repositories. each { repo ->
217+ if (repo. state == " open" ) {
218+ project. logger. lifecycle(" Promoting repository ${ repo.key} " )
219+
220+ def promoteUrl = new URL (" https://ossrh-staging-api.central.sonatype.com/manual/upload/repository/${ repo.key} ?publishing_type=automatic" )
221+ def connection = promoteUrl. openConnection()
222+ connection. setRequestMethod(" POST" )
223+ connection. setRequestProperty(" Authorization" , " Basic ${ token} " )
224+ connection. setRequestProperty(" Content-Type" , " application/json" )
225+
226+ def responseCode = connection. responseCode
227+ if (responseCode == 200 ) {
228+ project. logger. lifecycle(" Successfully promoted repository ${ repo.key} " )
229+ } else {
230+ def errorStream = connection. errorStream
231+ def errorBody = errorStream ? errorStream. text : " No error body available"
232+ def errorMessage = " Failed to promote repository ${ repo.key} . Response code: ${ responseCode} . Response message: ${ connection.responseMessage} . Error body: ${ errorBody} "
233+ project. logger. error(errorMessage)
234+ // throw new GradleException(errorMessage)
235+ }
236+ }
237+ }
238+ }
239+ }
240+ tasks. matching { it. name == ' publish' }. configureEach { publishTask ->
241+ publishTask. finalizedBy tasks. promoteToMavenCentral
242+ }
193243}
194244
195245def getGitCommit () {
0 commit comments