1+ import okhttp3.MediaType.Companion.toMediaType
2+ import okhttp3.OkHttpClient
3+ import okhttp3.RequestBody.Companion.asRequestBody
14import java.time.Duration
5+ import java.util.Base64
26
37plugins {
48 id(" idea" )
@@ -16,6 +20,12 @@ plugins {
1620 id(" org.graalvm.buildtools.native" ) apply false
1721}
1822
23+ buildscript {
24+ dependencies {
25+ classpath(" com.squareup.okhttp3:okhttp:4.12.0" )
26+ }
27+ }
28+
1929apply (from = " version.gradle.kts" )
2030
2131nexusPublishing {
@@ -111,6 +121,8 @@ if (gradle.startParameter.taskNames.contains("listTestsInPartition")) {
111121}
112122
113123tasks {
124+ val stableVersion = version.toString().replace(" -alpha" , " " )
125+
114126 val generateFossaConfiguration by registering {
115127 group = " Help"
116128 description = " Generate .fossa.yml configuration file"
@@ -141,4 +153,48 @@ tasks {
141153 }
142154 }
143155 }
156+
157+ val generateReleaseBundle by registering(Zip ::class ) {
158+ dependsOn(getTasksByName(" publishAllPublicationToReleaseRepoRepository" , true ))
159+ from(" releaseRepo" )
160+
161+ exclude(" **/maven-metadata.*" )
162+ duplicatesStrategy = DuplicatesStrategy .FAIL
163+ includeEmptyDirs = false
164+ archiveFileName.set(" release-bundle-$stableVersion .zip" )
165+ }
166+
167+ val uploadReleaseBundle by registering {
168+ dependsOn(generateReleaseBundle)
169+
170+ val bundle = generateReleaseBundle.get().outputs.files.singleFile
171+ val httpClient = OkHttpClient ()
172+ val username = System .getenv(" SONATYPE_USER" ) ? : throw GradleException (" Sonatype user not set" )
173+ val password = System .getenv(" SONATYPE_KEY" ) ? : throw GradleException (" Sonatype key not set" )
174+ val token = Base64 .getEncoder().encodeToString(" $username :$password " .toByteArray())
175+
176+ var query = " ?name=opentelemetry-java-instrumentation-$stableVersion "
177+ // uncomment to automatically publish the release
178+ // query += "&publishingType=AUTOMATIC"
179+
180+ val request = okhttp3.Request .Builder ()
181+ .url(" https://central.sonatype.com/api/v1/publisher/upload$query " )
182+ .post(
183+ okhttp3.MultipartBody .Builder ().addFormDataPart(
184+ " bundle" ,
185+ bundle.name,
186+ bundle.asRequestBody(" application/zip" .toMediaType())
187+ ).build()
188+ )
189+ .header(" authorization" , " Bearer $token " )
190+ .build()
191+ httpClient.newCall(request).execute().use { response ->
192+ response.body!! .string()
193+ }
194+
195+ httpClient.newCall(request).execute().use { response ->
196+ if (response.code != 201 ) throw GradleException (" Unexpected response status ${response.code} while uploading the release bundle" )
197+ println (" Uploaded deployment ${response.body!! .string()} " )
198+ }
199+ }
144200}
0 commit comments