Publish Java Packages to Maven Central Repository

 2 years ago
source link: https://www.devdungeon.com/content/publish-java-packages-maven-central-repository
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


You can push your own Java packages to Maven Repository - Central and make it available to everyone. It's a tricky process to set up the first time, but once you get it ready, it is incredibly easy to push updates. This tutorial will walk you through the basics of registering and pushing projects to Maven central using Sonatype's OSSRH (OSS Repository Hosting)

Helpful Links

Usually these go at the end but these are such valuable links, all your answers are probably found within these links:


Before pushing to the central repository you need to have proper access and a GPG key on a public server.

Sign up with Sonatype JIRA

Before publishing to the central repository, you must have a reserved namespace. The namespace is typically a domain name, but could also be a GitHub or BitBucket namespace like com.github.nanodano. To request your namespace, you need an account on the Sonatype JIRA first.

Sign up for Sonatype JIRA and follow the instructions to request your namespace.

Here is a link to my DevDungeon namespace request in Sonatype JIRA if it helps as a reference.

Create a GPG key

You will need a private GPG key to sign the code to verify you are the author and it has not been tampered with. Create a private key and save it somewhere secure and publish your public key to a trusted server.

If you don't know how to use GPG, check out my GPG Basics tutorial for a walkthrough of how to install gpg, generate keys, import and export, and push your private key to MIT's (and other) public key servers.

Here's a couple of the most important commands:

gpg --gen-key    # Create a new private key
gpg --list-keys  # To get the key id
gpg --export-secret-keys --armor <key_id> > private_gpg_key_backup.asc
gpg --keyserver hkp://pgp.mit.edu --send-keys <key_id>

Set up settings.xml

You can configure your GPG key and Sonatype password in the settings.xml file. The file should go inside your project directory next to your pom.xml or in $HOME/.m2/settings.xml. The server and profile provided here will be utilized in the pom.xml later.

Here is a sample settings.xml:

      <!-- Match the server id used in <distributionManagement> and <serverId>
           for sonatype staging plugin in `pom.xml` -->
        <!-- GPG should prompt for passphrase if not provided here. -->
        <!-- <gpg.passphrase>secretpassphrase</gpg.passphrase> -->
        <!-- <gpg.executable>/path/to/gpg.exe</gpg.executable> -->

Create a package that meets the requirements

Once you have been granted access in the Sonatype JIRA from the previous step, you need to create a package that meets the Sonatype package requirements before you can push it up to the central repository.

Some of the requirements include:

  • Required fields in the pom.xml
  • Javadoc and Sources (<package>-javadoc.jar, <package>-sources.jar)
  • GPG signature (*.asc for each file)
  • License info (e.g. MIT, Apache)
  • Developer information
  • Source code URLs

Building and deploying a package

You can run each of these steps individually or just run the last one which will call all previous ones in the build step.

mvn clean   # Delete build contents
mvn compile # Compile classes
mvn package # Build JAR
mvn verify  # Will sign/check GPG
mvn install # Install to local maven repo
mvn deploy  # Push to Sonatype->maven central

Sample project pom.xml

This is an example pom.xml for a Maven project that is intended to be published on Maven central. It has all of the plugins and required fields in the template. It also has the Sonatype repository addresses configured.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


  <description>An HTTP utility library</description>

      <name>The Apache License, Version 2.0</name>

      <email>[email protected]</email>


              <!-- GPG keyname var set in the settings.xml maven file -->
          <serverId>ossrh</serverId><!-- Server config in settings.xml -->

      <id>ossrh</id><!-- Server config in settings.xml -->
      <id>ossrh</id><!-- Server config in settings.xml -->




You should know have an understanding of how to publish a package to the Maven central repository using Sonatype's OSSRH.

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK