Skip to content

Commit

Permalink
support publishing through central portal (#733)
Browse files Browse the repository at this point in the history
* support publishing through central portal

* update changelog

* fix test setup

* rename to isCentralPortal
  • Loading branch information
gabrielittner committed Mar 10, 2024
1 parent e80a256 commit 2ccf77c
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.28.0 **UNRELEASED**

- Added support for publishing through the new [Central Portal](https://central.sonatype.com). To use
this use the `CENTRAL_PORTAL` option when specifying the Sonatype host.
- Updated minimum supported Gradle, Android Gradle Plugin and Kotlin versions.
- Removed support for the deprecated Kotlin/JS plugin.
- Removed the deprecated `closeAndReleaseRepository` task. Use `releaseRepository`, which
Expand Down
1 change: 1 addition & 0 deletions central-portal/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dependencies {
implementation(libs.moshi)
implementation(libs.retrofit)
implementation(libs.retrofit.converter.moshi)
implementation(libs.retrofit.converter.scalars)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import java.util.concurrent.TimeUnit
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory

class SonatypeCentralPortal(
private val baseUrl: String,
Expand All @@ -27,6 +27,7 @@ class SonatypeCentralPortal(
.writeTimeout(okhttpTimeoutSeconds, TimeUnit.SECONDS)
.build()
val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create())
.client(okHttpClient)
.baseUrl(baseUrl)
Expand All @@ -35,7 +36,7 @@ class SonatypeCentralPortal(
retrofit.create(SonatypeCentralPortalService::class.java)
}

private fun deleteDeployment(deploymentId: String) {
fun deleteDeployment(deploymentId: String) {
val deleteDeploymentResponse = service.deleteDeployment(deploymentId).execute()
if (!deleteDeploymentResponse.isSuccessful) {
throw IOException(
Expand All @@ -46,7 +47,7 @@ class SonatypeCentralPortal(
}
}

private fun publishDeployment(deploymentId: String) {
fun publishDeployment(deploymentId: String) {
val publishDeploymentResponse = service.publishDeployment(deploymentId).execute()
if (!publishDeploymentResponse.isSuccessful) {
throw IOException(
Expand Down Expand Up @@ -118,10 +119,9 @@ class SonatypeCentralPortal(
}
}

private fun upload(name: String?, publishingType: String?, file: File): String {
val uploadFile: RequestBody = file.asRequestBody("application/octet-stream".toMediaType())
val multipart =
MultipartBody.Part.createFormData("bundle", file.getName(), uploadFile)
fun upload(name: String?, publishingType: String?, file: File): String {
val uploadFile = file.asRequestBody("application/octet-stream".toMediaType())
val multipart = MultipartBody.Part.createFormData("bundle", file.name, uploadFile)
val uploadResponse = service.uploadBundle(name, publishingType, multipart).execute()
if (uploadResponse.isSuccessful) {
return uploadResponse.body()!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.vanniktech.maven.publish.portal
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query
import retrofit2.http.Streaming
Expand Down Expand Up @@ -50,7 +50,7 @@ internal interface SonatypeCentralPortalService {
fun uploadBundle(
@Query("name") name: String?,
@Query("publishingType") publishingType: String?,
@Body input: MultipartBody.Part,
@Part input: MultipartBody.Part,
): Call<String>

@Streaming
Expand Down
11 changes: 11 additions & 0 deletions docs/central.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ This can be done through either the DSL or by setting Gradle properties.
publishToMavenCentral(SonatypeHost.DEFAULT)
// or when publishing to https://s01.oss.sonatype.org
publishToMavenCentral(SonatypeHost.S01)
// or when publishing to https://central.sonatype.com/
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

signAllPublications()
}
Expand All @@ -91,6 +93,8 @@ This can be done through either the DSL or by setting Gradle properties.
publishToMavenCentral(SonatypeHost.DEFAULT)
// or when publishing to https://s01.oss.sonatype.org
publishToMavenCentral(SonatypeHost.S01)
// or when publishing to https://central.sonatype.com/
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

signAllPublications()
}
Expand All @@ -102,6 +106,8 @@ This can be done through either the DSL or by setting Gradle properties.
SONATYPE_HOST=DEFAULT
# or when publishing to https://s01.oss.sonatype.org
SONATYPE_HOST=S01
// or when publishing to https://central.sonatype.com/
SONATYPE_HOST=CENTRAL_PORTAL

RELEASE_SIGNING_ENABLED=true
```
Expand Down Expand Up @@ -253,6 +259,11 @@ lQdGBF4jUfwBEACblZV4uBViHcYLOb2280tEpr64iB9b6YRkWil3EODiiLd9JS3V...9pip+B1QLwEdL

## Publishing snapshots

!!! warning "Central Portal"

Publishing snapshots is not supported when using the Central Portal (central.sonatype.com).


Snapshots can be published by setting the version to something ending with `-SNAPSHOT`
and then running the following Gradle task:

Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ moshi-codegen = { module = "com.squareup.moshi:moshi-kotlin-codegen", version.re

retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-converter-moshi = { module = "com.squareup.retrofit2:converter-moshi", version.ref = "retrofit" }
retrofit-converter-scalars = { module = "com.squareup.retrofit2:converter-scalars", version.ref = "retrofit" }

junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
testParameterInjector = "com.google.testparameterinjector:test-parameter-injector-junit5:1.15"
Expand Down
2 changes: 2 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
compileOnly(libs.kotlin.plugin)
compileOnly(libs.android.plugin)

implementation(projects.centralPortal)
implementation(projects.nexus)

testImplementation(gradleTestKit())
Expand All @@ -60,6 +61,7 @@ val integrationTest by tasks.registering(Test::class) {
dependsOn(
tasks.publishToMavenLocal,
projects.nexus.dependencyProject.tasks.publishToMavenLocal,
projects.centralPortal.dependencyProject.tasks.publishToMavenLocal,
)
mustRunAfter(tasks.test)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ abstract class MavenPublishBaseExtension(
repositoryUsername = project.providers.gradleProperty("mavenCentralUsername"),
repositoryPassword = project.providers.gradleProperty("mavenCentralPassword"),
automaticRelease = automaticRelease,
// TODO: stop accessing rootProject https://github.com/gradle/gradle/pull/26635
rootBuildDirectory = project.rootProject.layout.buildDirectory,
)

val configCacheEnabled = project.configurationCache()
project.gradlePublishing.repositories.maven { repo ->
repo.name = "mavenCentral"
repo.setUrl(buildService.map { it.publishingUrl(configCacheEnabled) })
repo.credentials(PasswordCredentials::class.java)
if (!host.isCentralPortal) {
repo.credentials(PasswordCredentials::class.java)
}
}

val createRepository = project.tasks.registerCreateRepository(buildService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,36 @@ import java.io.Serializable
*
* https://central.sonatype.org/articles/2021/Feb/23/new-users-on-s01osssonatypeorg/
*/
data class SonatypeHost(
data class SonatypeHost internal constructor(
internal val rootUrl: String,
internal val isCentralPortal: Boolean,
) : Serializable {
constructor(rootUrl: String) : this(rootUrl, isCentralPortal = false)

internal fun apiBaseUrl(): String {
return "$rootUrl/service/local/"
return if (isCentralPortal) {
"$rootUrl/api/v1/"
} else {
"$rootUrl/service/local/"
}
}

companion object {
@JvmStatic
fun valueOf(sonatypeHost: String): SonatypeHost = when (sonatypeHost) {
"DEFAULT" -> DEFAULT
"S01" -> S01
"CENTRAL_PORTAL" -> CENTRAL_PORTAL
else -> throw IllegalArgumentException("No SonatypeHost constant $sonatypeHost")
}

@JvmField
val DEFAULT = SonatypeHost("https://oss.sonatype.org")
val DEFAULT = SonatypeHost("https://oss.sonatype.org", isCentralPortal = false)

@JvmField
val S01 = SonatypeHost("https://s01.oss.sonatype.org", isCentralPortal = false)

@JvmField
val S01 = SonatypeHost("https://s01.oss.sonatype.org")
val CENTRAL_PORTAL = SonatypeHost("https://central.sonatype.com", isCentralPortal = true)
}
}
Loading

0 comments on commit 2ccf77c

Please sign in to comment.