|
| 1 | +// Common Sonatype Central publishing functionality |
| 2 | +// This script can be applied by both publishing.gradle and publishing_aar.gradle |
| 3 | + |
| 4 | +// Custom task to upload deployment bundle to Sonatype Central API |
| 5 | +tasks.register("uploadToSonatypeCentral") { |
| 6 | + dependsOn publishToLocalMaven |
| 7 | + doLast { |
| 8 | + def sonatypeUsername = System.getenv("SONATYPE_USER") |
| 9 | + def sonatypePassword = System.getenv("SONATYPE_PASSWORD") |
| 10 | + |
| 11 | + if (!sonatypeUsername || !sonatypePassword) { |
| 12 | + throw new GradleException("SONATYPE_USER and SONATYPE_PASSWORD environment variables must be set") |
| 13 | + } |
| 14 | + |
| 15 | + // Create deployment bundle (zip file containing all artifacts) |
| 16 | + def bundleFile = new File(project.buildDir, "distributions/deployment-bundle.zip") |
| 17 | + bundleFile.parentFile.mkdirs() |
| 18 | + |
| 19 | + ant.zip(destfile: bundleFile) { |
| 20 | + fileset(dir: "${project.buildDir}/repo") { |
| 21 | + include(name: "**/*") |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Create base64 encoded credentials |
| 26 | + def credentials = "${sonatypeUsername}:${sonatypePassword}" |
| 27 | + def encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()) |
| 28 | + |
| 29 | + // Upload bundle to Sonatype Central API |
| 30 | + def connection = new URL("https://central.sonatype.com/api/v1/publisher/upload").openConnection() |
| 31 | + connection.setRequestMethod("POST") |
| 32 | + connection.setRequestProperty("Authorization", "Bearer ${encodedCredentials}") |
| 33 | + connection.setRequestProperty("Content-Type", "multipart/form-data") |
| 34 | + connection.setDoOutput(true) |
| 35 | + |
| 36 | + // Set up multipart form data |
| 37 | + def boundary = "----WebKitFormBoundary" + System.currentTimeMillis() |
| 38 | + connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary) |
| 39 | + |
| 40 | + def outputStream = connection.getOutputStream() |
| 41 | + def writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true) |
| 42 | + |
| 43 | + // Add file part |
| 44 | + writer.append("--" + boundary).append("\r\n") |
| 45 | + writer.append("Content-Disposition: form-data; name=\"bundle\"; filename=\"" + bundleFile.name + "\"").append("\r\n") |
| 46 | + writer.append("Content-Type: application/zip").append("\r\n") |
| 47 | + writer.append("\r\n") |
| 48 | + writer.flush() |
| 49 | + |
| 50 | + // Write file content |
| 51 | + bundleFile.withInputStream { inputStream -> |
| 52 | + outputStream << inputStream |
| 53 | + } |
| 54 | + outputStream.flush() |
| 55 | + |
| 56 | + writer.append("\r\n") |
| 57 | + writer.append("--" + boundary + "--").append("\r\n") |
| 58 | + writer.close() |
| 59 | + |
| 60 | + // Get response |
| 61 | + def responseCode = connection.getResponseCode() |
| 62 | + if (responseCode == 200 || responseCode == 201) { |
| 63 | + println "Bundle uploaded successfully to Sonatype Central" |
| 64 | + def response = connection.getInputStream().getText() |
| 65 | + println "Response: ${response}" |
| 66 | + } else { |
| 67 | + def errorResponse = connection.getErrorStream()?.getText() ?: "No error details available" |
| 68 | + throw new GradleException("Failed to upload bundle. Response code: ${responseCode}, Error: ${errorResponse}") |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Task to publish snapshots to Sonatype Central Portal |
| 74 | +tasks.register("publishSnapshotToCentral") { |
| 75 | + description = "Publishes snapshot artifacts to Sonatype Central Portal" |
| 76 | + group = "publishing" |
| 77 | + |
| 78 | + doFirst { |
| 79 | + // Ensure version ends with -SNAPSHOT |
| 80 | + if (!project.version.toString().endsWith('-SNAPSHOT')) { |
| 81 | + throw new GradleException("Version must end with -SNAPSHOT to publish as snapshot. Current version: ${project.version}") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + dependsOn "publishAllPublicationsToCentralSnapshotRepository" |
| 86 | +} |
| 87 | + |
| 88 | +// Task to publish artifacts to local Maven repository |
| 89 | +tasks.register("publishToLocalMavenRepository") { |
| 90 | + description = "Publishes all artifacts to local Maven repository (~/.m2/repository)" |
| 91 | + group = "publishing" |
| 92 | + |
| 93 | + dependsOn "publishToMavenLocal" |
| 94 | + |
| 95 | + doLast { |
| 96 | + println "Artifacts published to local Maven repository at: ${System.getProperty('user.home')}/.m2/repository" |
| 97 | + println "You can now use the artifacts in other local projects with version: ${project.version}" |
| 98 | + } |
| 99 | +} |
0 commit comments