Publishing a Java Library to Maven Central with Gradle
Fast recall
Quick revision
Core answer
maven-publish plugin to define a MavenPublication, sign artifacts with the signing plugin using a GPG key, configure the Sonatype OSSRH staging repository, and run ./gradlew publishToSonatype closeSonatypeStagingRepository to release to Maven Central.Whether you have experience publishing reusable open-source libraries and understand the requirements Maven Central imposes.
Not including sources and Javadoc JARs, which Maven Central requires alongside the main artifact, or not signing artifacts with GPG.
Mention using the `io.github.gradle-nexus.publish-plugin` to automate staging and promotion, signing with an in-memory key for CI environments, and semantic versioning with automated changelog.
Interview-ready guide
Interview answer
Publishing to Maven Central is more involved than publishing to an internal Nexus because Sonatype enforces strict requirements that most internal registries do not — complete POM metadata, sources JAR, Javadoc JAR, and GPG signatures on every artifact.
The three required plugins
maven-publishdefines the publication and POM metadata — license, developer, SCM URL are all required by Sonatype's validation; a missing field fails the staging close step after uploadsigningapplies GPG signatures to every artifact; without signatures, Sonatype rejects the staging repositoryio.github.gradle-nexus.publish-pluginautomates the Sonatype staging workflow: it creates a staging repository, uploads, closes, and releases in sequence without manual Sonatype UI interaction
Sources and Javadoc — the most commonly missed requirement
- Call
java.withSourcesJar()andjava.withJavadocJar()in thejava {}block — without these, the upload succeeds but staging close fails because Sonatype's validation rejects the incomplete submission - Sonatype requires the sources JAR so that IDEs can show source code for library consumers; the Javadoc JAR enables documentation lookup
- The staging close failure message is not always obvious — running with
--infoexposes the exact validation error from Sonatype's API response
CI release workflow
- GPG private key and OSSRH credentials must be environment variables in CI —
useInMemoryPgpKeys(findProperty("signingKey"), findProperty("signingPassword"))reads the key from a property that CI sets from a secret; no GPG keyring needed on the CI runner - Tie releases to Git tags: pushing
v1.0.0triggers the CI pipeline that runs the full publish-close-release sequence; no manual steps - Always publish a snapshot to
https://s01.oss.sonatype.org/content/repositories/snapshotsfirst to verify the pipeline works before attempting the first production release
Publishing a Java Library to Maven Central with Gradle
Direct Answer
Use the maven-publish plugin to define a MavenPublication, sign artifacts with the signing plugin using a GPG key, configure the Sonatype OSSRH staging repository, and run ./gradlew publishToSonatype closeSonatypeStagingRepository to release to Maven Central.
Common Mistakes & Pitfalls
Not including sources and Javadoc JARs, which Maven Central requires alongside the main artifact, or not signing artifacts with GPG.
Check your understanding
Answer these without looking back at the article.
- How do you publish a SNAPSHOT version to OSSRH for testing before a release?
- How do you automate the Maven Central release process in GitHub Actions?
- What is the difference between OSSRH and the newer Maven Central Portal?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.