Publishing to Maven Central using OSSRH

I had a period where I thought I might not be good enough to publish.

Stephen King

This article summarizes the set of steps which should be completed to publish a package to Maven Central. It uses a combination of the maven-release and nexus-staging-maven plugins to accomplish this. A good example of the end result can be found in the dwolla-java respository on Github.

Distributing your Public Key

Artifacts need to be GPG signed. Generate a key if you have not already done so.

Send your public key to a keyserver so other people can use it to verify your files. To do this you will need to determine your key-id. These ids found on the left-hand-side of the output printed out below directly to the right of any sub instances:

$ gpg --list-keys
pub   2048R/AAAAAAAA 2014-01-10
uid                  Corey Oliver   <>
sub   2048R/BBBBBBBB 2014-01-10

pub   2048R/CCCCCCCC 2014-11-24
uid                  Corey Oliver <>
sub   2048R/DDDDDDDD 2014-11-24

For example, if you wanted to send the key associated with, you would run:

$ gpg --keyserver --send-keys CCCCCCCC

Optional GPG Configuration

If you have more than one GPG key pair, you can specify which one the maven-release plugin will use by editing your settings.xml file. Add the following:


Where project-name is the name of your project and CCCCCCCC is your GPG key-id.

Configuring OSSRH Credentials

Configure your settings.xml file to have the appropriate Sonatype OSSRH credentials. Specifically, this is your OSSRH Jira account. For instructions on creating a Jira account, refer to the to the OSSRH Guide.


For security reasons, you will likely wish to encrypt your Jira password.

Editing the POM

Now it is time to edit the pom.xml file for your project.

Add the distributionManagement element. Note the id specified for the following repositories must match the id of the server given in your settings.xml file.


Add the maven-release plugin to the pluginManagement element. The maven-release plugin is used to automatically generate what is often mistake ridden commits and tags associated with a packages release.

Note the following customizations:

  1. pushChanges is set to false. This means maven-release is not configured to automatically push up commits or tags it creates to the remote. This avoids potential issues that arise when the release fails, and having to manually fix upstream history.
  2. localCheckout is set to true. Meaning a local checkout is used instead of doing one from the upstream repository. This is required because automatic pushing is turned off.
  3. arguments is set to -Pproject-name-release. This activates the project-name-release profile when the maven-release plugin is used.

Where project-name is the name of your project.

Next, add the nexus-staging-maven plugin which is used to control Nexus Staging workflow.


Finally, add the project-name-release profile for signing with your GPG key.


Publishing a Snapshot

To publish a snapshot of your project run:

$ mvn clean deploy

Performing a Release

Complete the following to perform a release:

$ mvn release:clean release:prepare

If for any reason prepare fails, run:

$ mvn release:rollback


$ mvn release:perform

Push up the tag and commits generated by the maven-release plugin:

$ git push origin master && git push origin <tag-name>


Generating a GPG Key Pair

Install GPG and execute the following command:

$ gpg --gen-key

Select the default value when asked for the type, then size, and the time of validity for the key. Provide additional information as appropriate including a passphrase.

Though not required, it is wise to backup up the generated keys and passphrase in a password manager.

Backing up GPG Keys

To export the keys associated with you would run:

$ gpg --export -a "" > public.key
$ gpg --export-secret-key -a "" > private.key

The corresponding commands to import the keys would be:

$ gpg --import public.key
$ gpg --allow-secret-key-import --import private.key

Encrypting Passwords for Maven

Create a master password by running:

$ mvn -emp master-password

Where master-password is your master password.

A encrypted copy of the password will be printed out. Copy this to !/.m2/settings-security.xml.


Now, run the following to encrypt your particular password:

$ mvn -ep password

This will be the value that you provide in the desired password element.