Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.0.8 #4

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 65 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,103 +1,129 @@
# Sonatype Maven Central publish plugin

This plugin simplifies publishing your artifacts to [Sonatype Maven Central](https://central.sonatype.org).
It uses the standard plugins such as `org.gradle.maven-publish` and `org.gradle.signing`.
[![Gradle Plugin Portal](https://img.shields.io/gradle-plugin-portal/v/dev.g000sha256.sonatype-maven-central?logo=gradle&label=Gradle%20Plugin%20Portal&labelColor=02303A&color=blue)](https://plugins.gradle.org/plugin/dev.g000sha256.sonatype-maven-central)

## Usage
This plugin simplifies the process of publishing your artifacts to the
[Sonatype Maven Central](https://central.sonatype.org) repository. It utilizes standard plugins
such as the [Maven Publish Plugin](https://docs.gradle.org/current/userguide/publishing_maven.html)
and the [Signing Plugin](https://docs.gradle.org/current/userguide/signing_plugin.html).

## Initialization

### Add plugin repository

```kotlin
repositories {
gradlePluginPortal()
pluginManagement {
repositories {
gradlePluginPortal()
}
}
```

### Add Sonatype plugin and necessary plugins
### Apply plugin

```kotlin
plugins {
id("dev.g000sha256.sonatype-maven-central") version "0.0.7"
id("org.gradle.maven-publish")
id("org.gradle.signing")
}
```

### Configure Sonatype plugin
> [!NOTE]
> The plugins `org.gradle.maven-publish` and `org.gradle.signing` will be applied automatically,
> so you don't need to add them manually.

## Configuration

### Choose a publishing type

The plugin can have two types of publishing:

- `SonatypeMavenCentralType.Manual` (default) - a deployment will go through validation and require
the user to manually publish it via the [Portal UI](https://central.sonatype.com/publishing/deployments)
- `SonatypeMavenCentralType.Automatic` - a deployment will go through validation and, if it passes,
will be automatically published to `Maven Central`

You can override the type using a plugin extension:

```kotlin
import g000sha256.sonatype_maven_central.SonatypeMavenCentralType

sonatypeMavenCentralRepository {
type = SonatypeMavenCentralType.Manual
}
```

### Set up Sonatype credentials

Store your [Sonatype credentials](https://central.sonatype.org/publish/generate-portal-token)
securely in your private `Gradle` properties file (`~/.gradle/gradle.properties`):

```properties
SonatypeMavenCentral.Username=<your sonatype username>
SonatypeMavenCentral.Password=<your sonatype password>
```

You can override the credentials using a plugin extension:

```kotlin
sonatypeMavenCentralRepository {
credentials {
username = properties["SonatypeMavenCentral.Username"] as String?
password = properties["SonatypeMavenCentral.Password"] as String?
username = "<your sonatype username>"
password = "<your sonatype password>"
}
}
```

You can configure the plugin by selecting one of the following types for publishing:

- `SonatypeMavenCentralType.Manual` (default) - a deployment will go through validation and require the user to publish manually
it via the [Portal UI](https://central.sonatype.com/publishing/deployments)
- `SonatypeMavenCentralType.Automatic` - a deployment will go through validation and, if it passes, automatically proceed to
publish to Maven Central

### Configure necessary plugins
### Register Maven publication

```kotlin
publishing {
publications {
register<MavenPublication>("<your publication variant>") {
// configuration
groupId = "<your publication group id>"
artifactId = "<your publication artifact id>"
version = "<your publication version>"

// pom/component/artifacts configuration
}
}
}

signing {
val publication = publishing.publications["<your publication variant>"]
sign(publication)
}
```

### Configure GPG credentials
### Configure signing

Store your [GPG credentials](https://central.sonatype.org/publish/requirements/gpg) securely in your private Gradle properties
file (`~/.gradle/gradle.properties`):
Store your [GPG credentials](https://central.sonatype.org/publish/requirements/gpg)
securely in your private `Gradle` properties file (`~/.gradle/gradle.properties`):

```properties
signing.keyId=<your signing keyId>
signing.password=<your signing password>
signing.secretKeyRingFile=<your path to secring.gpg file>
```

### Configure Sonatype credentials
Sign the publication:

Store your [Sonatype credentials](https://central.sonatype.org/publish/generate-portal-token) securely in your private Gradle
properties file (`~/.gradle/gradle.properties`):

```properties
SonatypeMavenCentral.Username=<your sonatype username>
SonatypeMavenCentral.Password=<your sonatype password>
```kotlin
signing {
val publication = publishing.publications["<your publication variant>"]
sign(publication)
}
```

### Publishing
## Publishing

#### Publish all
### Publish all variants to all repositories

```shell
./gradlew publish
```

#### Publish all variants to the Sonatype repository
### Publish all variants to the Sonatype repository

```shell
./gradlew publishAllPublicationsToSonatypeMavenCentralRepository
```

#### Publish a specific variant to the Sonatype repository
### Publish a specific variant to the Sonatype repository

```shell
./gradlew publish<your publication variant>PublicationToSonatypeMavenCentralRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import org.gradle.api.provider.Property
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.plugins.signing.SigningPlugin

internal fun Project.initPlugin(
usernameProperty: Property<String>,
passwordProperty: Property<String>,
typeProperty: Property<SonatypeMavenCentralType>
) {
plugins.apply(MavenPublishPlugin::class.java)
plugins.apply(SigningPlugin::class.java)

val buildDirectory = layout.buildDirectory.asFile.get()
val pluginDirectory = createDirectory(buildDirectory, "sonatype_maven_central")
Expand All @@ -51,8 +53,8 @@ internal fun Project.initPlugin(

tasks.named("publish${variant}PublicationToSonatypeMavenCentralRepository") {
it.doLast {
val username = usernameProperty.getTrimmedValue()
val password = passwordProperty.getTrimmedValue()
val username = usernameProperty.getTrimmedValue() ?: getTrimmedProperty("SonatypeMavenCentral.Username")
val password = passwordProperty.getTrimmedValue() ?: getTrimmedProperty("SonatypeMavenCentral.Password")

requireNotNull(username) { "Username is null or blank" }
requireNotNull(password) { "Password is null or blank" }
Expand All @@ -73,17 +75,21 @@ internal fun Project.initPlugin(
}

private fun Property<String>.getTrimmedValue(): String? {
val rawValue = getOrNull()
if (rawValue == null) {
return null
}
val value = getOrNull()
return value?.trimOrNull()
}

val trimmedValue = rawValue.trim()
if (trimmedValue.length == 0) {
private fun Project.getTrimmedProperty(key: String): String? {
val value = properties[key] as String?
return value?.trimOrNull()
}

private fun String.trimOrNull(): String? {
val value = trim()
if (value.length == 0) {
return null
}

return trimmedValue
return value
}

private fun Property<SonatypeMavenCentralType>.getStringType(): String {
Expand Down