/sonatype-maven-central

Unofficial Gradle plugin for publishing to the Sonatype Maven Central repository

Primary LanguageKotlinApache License 2.0Apache-2.0

Maven Central publish plugin

Gradle Plugin Portal

This Gradle plugin simplifies the process of publishing your artifacts to the Sonatype Maven Central Repository. It utilizes standard plugins such as the Maven Publish Plugin and the Signing Plugin.

Initialization

Add plugin repository

pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
}

Apply plugin

plugins {
    id("dev.g000sha256.sonatype-maven-central") version "1.0.0"
}

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
  • 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 as 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 username>
SonatypeMavenCentral.Password=<your sonatype password>

You also can override the credentials using a plugin extension:

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

Register Maven publication

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

            // pom/component/artifacts configuration
        }
    }
}

Configure signing

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

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

Sign the publication:

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

Publishing

Publish all variants to all repositories

./gradlew publish

Publish all variants to the Sonatype repository

./gradlew publishAllPublicationsToSonatypeMavenCentralRepository

Publish a specific variant to the Sonatype repository

./gradlew publish<your publication variant>PublicationToSonatypeMavenCentralRepository