This simple plugin builds and uploads existing maven publication to Maven Central Repository using Portal Publisher API
Tested on Gradle 8.14
- Add and apply plugin
- Configure extension:
// add imports
import xyz.bobkinn.sonatypepublisher.PublishingType
import xyz.bobkinn.sonatypepublisher.sonatypePublish
sonatypePublish {
// specify maven central repository token username and password
username = System.getenv("MAVEN_CENTRAL_USERNAME")
password = System.getenv("MAVEN_CENTRAL_PASSWORD")
publishingType = PublishingType.USER_MANAGED // or AUTOMATIC so deployments released when ready
// using shortcut method to register central publish for maven publication and same name
registerMaven(publishing.publications.named("main", MavenPublication::class))
}- Configure your publication with correct POM, sources, javadocs and signing to match maven central requirements
- Run task
publish<publication name>ToSonatype
Java 17 or later required
Setup Gradle Plugin Portal plugin repository in settings.gradle.kts:
pluginManagement {
repositories {
gradlePluginPortal() // add if not already
}
}Or setup using JitPack - How To section.
Add then apply plugin in build.gradle.kts:
plugins {
id("io.github.bobkinn.sonatype-publisher") version "2.2.4"
}Based on sonatype-maven-central-publisher, I have introduced critical changes, code improvements and fixes, targeting different goal to work with existing publications.
Fixed issues:
There are also more plans for plugin in TODO file
Each publication can be configured via the SonatypePublishExtension using the following options:
-
publishingType(AUTOMATIC|USER_MANAGED) Determines how the publication is uploaded to Sonatype. -
additionalTasks(List<String>) Extra tasks to run when building artifacts for this publication.Currently, this tasks results are not included in bundle
-
additionalAlgorithms(List<String>) Extra hash algorithms to include in the bundle.MD5 and SHA-1 are always included.
-
username/password(String) Sonatype credentials for authentication. -
publication(MavenPublication) The Gradle publication whose artifacts will be built and published.
Registering a Maven publication:
sonatypePublish.registerMaven("myLib", myPublication) {
it.publishingType = PublishingType.AUTOMATIC
it.additionalTasks = listOf("someTask")
it.additionalAlgorithms = listOf("SHA-256")
}- Credentials and other internal settings are handled automatically by the plugin.
- Users usually only need to specify the publication and optional additional tasks or hash algorithms.
These tasks provide manual control and bulk automation for Sonatype Portal deployments.
They are not bound to specific project configurations β instead they operate using stored deployment IDs and Sonatype credentials defined in your SonatypePublishExtension.
Deployments statuses is stored in <root project>/build/sonatypePublish/deployments.json:
current - currently unpublished deployments. They are used in tasks
published - published deployments, exists just for informational purposes
All tasks use credentials defined in your plugin extension:
sonatypePublish {
username = "your-username"
password = "your-password"
}Fetches the latest status of stored deployments and prints their current state.
What it does
- Updates deployment statuses from Sonatype Portal
- Prints details (state, name, errors if present)
- Can target a specific deployment or all stored ones
Usage
# Check all current deployments
./gradlew checkDeployments
# Check a specific deployment
./gradlew checkDeployments -PdeploymentId=<deploymentId>Behavior
- Removes deployments no longer present on the portal
- Moves published deployments to the published list
- Updates statuses for active deployments
Publishes a specific deployment using its deployment ID.
Usage
./gradlew publishDeployment -PdeploymentId=<deploymentId>Behavior
- Triggers publish through the Sonatype Portal API
- Updates stored deployment state to
PUBLISHING - Fails if
deploymentIdis missing or blank
Drops a specific deployment from the portal.
Usage
./gradlew dropDeployment -PdeploymentId=<deploymentId>Behavior
- Calls the Portal API to drop the deployment
- Removes it from the stored current deployment list
Fetches latest statuses and automatically drops all failed deployments.
Usage
./gradlew dropFailedDeploymentsBehavior
- Updates deployment status first
- Drops only deployments marked as failed
- Saves updated deployment data afterward
Fetches latest statuses and publishes all validated deployments automatically.
Usage
./gradlew publishValidatedDeploymentsBehavior
- Updates deployment status first
- Publishes deployments marked as validated
- Updates their state to
PUBLISHING - Saves updated deployment data
This pipeline builds and publishes a Maven publication to Sonatype Central. It runs automatically when executing the final publish task.
Steps:
-
BuildPublicationArtifacts
- Builds all artifacts for the publication.
- Depends on underlying build tasks and any extra configured tasks.
-
AggregateFiles
- Copies all artifacts to a temporary directory.
- Renames files to standard Maven conventions.
-
ComputeHashes
- Generates MD5 and SHA-1 hashes for all aggregated files.
- Optional additional hash algorithms can be configured.
-
CreateZip
- Packages all files and hashes into a single zip archive for upload.
-
PublishToSonatypeCentral
- Uploads the zip bundle to Sonatype Nexus.
- Saves the deployment ID for tracking.
Pipeline Flow:
buildArtifacts β aggregateFiles β computeHashes β createZip β publishToSonatype