Skip to content

Repository files navigation

Maven Central publish plugin

Maven Central

This is an unofficial Gradle plugin that simplifies the process of publishing your artifacts to the Sonatype Maven Central Repository. It uses the standard Maven Publish plugin and Signing plugin, and auto-applies the Gradle Signing plugin for in-memory PGP keys.

Initialization

Add plugin repository

pluginManagement {
    repositories {
        mavenCentral()
    }
}

Apply plugin

plugins {
    id("dev.g000sha256.sonatype-maven-central") version "<latest>"
}

Note

The plugins org.gradle.maven-publish, org.gradle.signing, and dev.g000sha256.signing will be applied automatically, so you don't need to add them manually.

Configuration

Choose a publishing type

The plugin supports two publishing types:

  • SonatypeMavenCentralType.Manual (default) - a deployment will go through validation and require the user to manually publish it via the Portal UI
  • SonatypeMavenCentralType.Automatic - a deployment will go through validation and, if it passes, will be automatically published to Maven Central

By default, the type is set to Manual, but you can override it using a plugin extension:

import g000sha256.sonatype_maven_central.SonatypeMavenCentralType

sonatypeMavenCentralRepository {
    type = SonatypeMavenCentralType.Automatic
}

Set up Sonatype credentials

Store your Sonatype credentials securely in your private Gradle properties file (~/.gradle/gradle.properties):

SonatypeMavenCentral.Username=<your sonatype portal username>
SonatypeMavenCentral.Password=<your sonatype portal password>

For CI/CD, the plugin also reads the credentials from environment variables:

SONATYPE_USERNAME=<your sonatype portal username>
SONATYPE_PASSWORD=<your sonatype portal password>

You can also override the credentials using a plugin extension:

sonatypeMavenCentralRepository {
    credentials {
        username = "<your sonatype portal username>"
        password = "<your sonatype portal password>"
    }
}

Note

The plugin reads credentials in the following order: extension, then environment variable, then Gradle property.

Register Maven publication

publishing {
    publications {
        register<MavenPublication>("<your publication variant>") {
            // your publication configuration
        }
    }
}

Configure signing

There are two steps: add signing keys and choose what to sign.

Add keys

The plugin auto-applies the Gradle Signing plugin for in-memory PGP keys.

For CI/CD, set environment variables:

SIGNING_KEY=<your signing key>
SIGNING_PASSWORD=<your signing password>
# optional
SIGNING_KEY_ID=<your signing key id>

Or store the credentials in your private Gradle properties file (~/.gradle/gradle.properties):

signing.key=<your signing key>
signing.password=<your signing password>
# optional
signing.keyId=<your signing key id>

Note

If those values aren't resolved, the standard Signing plugin's file-based GPG credentials (signing.keyId, signing.password, signing.secretKeyRingFile) still work.

You can also override the configured keys via your own signing block:

signing {
    useInMemoryPgpKeys("<your signing key>", "<your signing password>")
}

Sign

A specific publication:

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

or all publications:

signing {
    sign(publishing.publications)
}

See signing publications in the Gradle docs for more options.

Publishing

./gradlew publish

or

./gradlew publishAllPublicationsToSonatypeMavenCentralRepository

or

./gradlew publish<your publication variant>PublicationToSonatypeMavenCentralRepository

Releases

Used by

Contributors

Languages