@@ -68,13 +68,54 @@ publishing {
6868
6969 repositories {
7070 maven {
71- name = ' MavenCentral'
72- url = ' https://central.sonatype.com/api/v1/publisher/upload'
73- credentials {
74- username = project. findProperty(' mavenCentralUsername' ) ?: System . getenv(' MAVEN_CENTRAL_USERNAME' )
75- password = project. findProperty(' mavenCentralToken' ) ?: System . getenv(' MAVEN_CENTRAL_TOKEN' )
76- }
71+ name = ' LocalRepo'
72+ url = layout. buildDirectory. dir(' maven-repo' )
73+ }
74+ }
75+ }
76+
77+ // Task to create a bundle for Maven Central Portal
78+ tasks. register(' createCentralBundle' , Zip ) {
79+ dependsOn ' publishMavenJavaPublicationToLocalRepoRepository'
80+ from layout. buildDirectory. dir(' maven-repo' )
81+ archiveFileName = " central-bundle.zip"
82+ destinationDirectory = layout. buildDirectory. dir(' distributions' )
83+ }
84+
85+ // Task to upload bundle to Maven Central Portal
86+ tasks. register(' publishToMavenCentral' ) {
87+ dependsOn ' createCentralBundle'
88+
89+ doLast {
90+ def username = project. findProperty(' mavenCentralUsername' ) ?: System . getenv(' MAVEN_CENTRAL_USERNAME' )
91+ def token = project. findProperty(' mavenCentralToken' ) ?: System . getenv(' MAVEN_CENTRAL_TOKEN' )
92+
93+ if (! username || ! token) {
94+ throw new GradleException (' Maven Central credentials not found. Set MAVEN_CENTRAL_USERNAME and MAVEN_CENTRAL_TOKEN environment variables.' )
95+ }
96+
97+ def credentials = " ${ username} :${ token} " . bytes. encodeBase64(). toString()
98+ def bundleFile = layout. buildDirectory. file(' distributions/central-bundle.zip' ). get(). asFile
99+
100+ def uploadUrl = ' https://central.sonatype.com/api/v1/publisher/upload?publishingType=AUTOMATIC'
101+
102+ println " Uploading bundle to Maven Central Portal..."
103+
104+ def process = [
105+ ' curl' , ' -X' , ' POST' ,
106+ ' -H' , " Authorization: Bearer ${ credentials} " ,
107+ ' -F' , " bundle=@${ bundleFile.absolutePath} " ,
108+ uploadUrl
109+ ]. execute()
110+
111+ process. waitForProcessOutput(System . out, System . err)
112+ def exitCode = process. exitValue()
113+
114+ if (exitCode != 0 ) {
115+ throw new GradleException (" Failed to upload bundle to Maven Central. Exit code: ${ exitCode} " )
77116 }
117+
118+ println " Bundle uploaded successfully!"
78119 }
79120}
80121
0 commit comments