1+ import groovy.json.JsonSlurper
2+
13apply plugin : ' maven-publish'
24apply plugin : ' signing'
35
@@ -11,13 +13,13 @@ def isReleaseBuild() {
1113def getReleaseRepositoryUrl () {
1214 return hasProperty(' RELEASE_REPOSITORY_URL' )
1315 ? RELEASE_REPOSITORY_URL
14- : " https://oss. sonatype.org /service/local/staging/deploy/maven2/"
16+ : " https://ossrh-staging-api.central. sonatype.com /service/local/staging/deploy/maven2/"
1517}
1618
1719def getSnapshotRepositoryUrl () {
1820 return hasProperty(' SNAPSHOT_REPOSITORY_URL' )
1921 ? SNAPSHOT_REPOSITORY_URL
20- : " https://oss. sonatype.org /content/repositories/snapshots/"
22+ : " https://ossrh-staging-api.central. sonatype.com /content/repositories/snapshots/"
2123}
2224
2325def getRepositoryUsername () {
@@ -168,4 +170,51 @@ afterEvaluate {
168170 }
169171 }
170172 }
171- }
173+
174+ tasks. register(' moveFromOSSRHToCentralPublisherPortal' ) {
175+ doLast {
176+ def repoKey = get(' search/repositories' ). repositories[0 ]. key
177+ post(" upload/repository/$repoKey " )
178+ }
179+ }
180+
181+ tasks. withType(PublishToMavenRepository ) {
182+ finalizedBy(' moveFromOSSRHToCentralPublisherPortal' )
183+ }
184+ }
185+
186+ def request (path , method , body = null ) {
187+ def url = " https://ossrh-staging-api.central.sonatype.com/manual/$path "
188+ def con = new URI (" https://ossrh-staging-api.central.sonatype.com/manual/$path " ). toURL(). openConnection()
189+ def str = new String (Base64 . encoder. encode(" ${ getRepositoryUsername()} :${ getRepositoryPassword()} " . getBytes()))
190+ con. addRequestProperty(' Authorization' , " Bearer $str " )
191+ con. setRequestMethod(method)
192+ if (method == " POST" || method == " PUT" ) {
193+ con. setDoOutput(true )
194+ }
195+ if (body != null ) {
196+ con. outputStream. write(body. getBytes())
197+ }
198+ def responseCode = con. getResponseCode()
199+ if (responseCode >= 200 && responseCode <= 299 ) {
200+ def responseBody = con. inputStream. getText()
201+ if (responseBody != null && ! responseBody. isEmpty()) {
202+ return new JsonSlurper (). parseText(responseBody)
203+ }
204+ } else {
205+ def errorBody = con?. errorStream?. getText()
206+ if (errorBody != null && ! errorBody. isEmpty()) {
207+ throw new RuntimeException (" $method $url failed: code=$responseCode , body=$errorBody " )
208+ } else {
209+ throw new RuntimeException (" $method $url failed: code=$responseCode " )
210+ }
211+ }
212+ }
213+
214+ def get (path ) {
215+ return request(path, ' GET' )
216+ }
217+
218+ def post (path , body = null ) {
219+ return request(path, ' POST' , body)
220+ }
0 commit comments