Skip to content

Commit c890544

Browse files
Implement tasks for creating and publishing a bundle to Maven Central; update GitHub Actions workflow to use new publish task.
1 parent 677611c commit c890544

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
3434

3535
- name: Publish to Maven Central
36-
run: ./gradlew publish
36+
run: ./gradlew publishToMavenCentral
3737
env:
3838
MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
3939
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}

build.gradle

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)