This blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://blog.xeiam.com
and update your bookmarks.

Wednesday, March 6, 2013

A Practical Guide to Uploading Artifacts to Maven Central

This blog post takes you through the steps you need to take to get your Java artifacts (Jars, JavaDocs, Sources, etc.) uploaded to Maven Central. A lot of the steps are already well documented on Sonatype's Maven Repository Usage Guide, and this post will refer to it heavily but fill in the spots where that guide is a bit weak. First off, here are the main references you will need:

References

https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide https://docs.sonatype.org/display/Repository/How+To+Generate+PGP+Signatures+With+Maven

Heads Up Mac Users!

If you get a complaint from Maven about Java Home not being properly set on your Mac do this at the CLI:
 echo $JAVA_HOME  
 export JAVA_HOME=$(/usr/libexec/java_home)  

Instructions

Follow the instructions in the first reference up to, but not including step 7a.3 Stage a Release. At this point you've already gotten a Sonatype account, pushed snapshots to Sonatype's snapshots repo, and got your PGP keys setup on your machine. Now to upload your non-snapshot releases to Maven Central, you need to run some Maven commands from the CLI. Make sure you have Git installed on your machine as well. The following Maven commands work for multi-module Maven projects as well as single-jar projects. You'll need to adapt these commands to your specific project. These instructions are just a supplement to step 7a.3 on Sonatype's guide. Here are the CLI commands I ran to release XChange version 1.3.0...

Tip!

Leave the -SNAPSHOT in your pom.xml. The release plugin removes it and increments to the next snapshot version for you automatically. It also tags the commit for you too.

First Do a Dry Run

note: replace "xchange" with your own artifactId, replace 1.3.0 with your current version.
 mvn clean  
 mvn release:clean  
 mvn --batch-mode -Dtag=xchange-1.3.0 -DreleaseVersion=1.3.0 -DdevelopmentVersion=1.3.1-SNAPSHOT -DdryRun=true release:prepare  
 mvn release:rollback  

Then Do the Real Thing

note: replace "xchange" with your own artifactId, replace 1.3.0 with your current version.
 mvn release:clean  
 mvn --batch-mode -Dtag=xchange-1.3.0 -DreleaseVersion=1.3.0 -DdevelopmentVersion=1.3.1-SNAPSHOT release:prepare  
 mvn release:perform -Darguments=-Dgpg.passphrase=PASSPHRASE  

The Final Stretch

The last thing to do now is to use the Sonatype web interface to release the artifacts. Just follow the instructions starting at Step 8a on Sonatype's Guide.

Done!

After a couple hours of waiting, your artifacts will be available on Maven Central. I found that it takes even longer for them to be available on search.maven.org, but as soon as they show up in the repository, you and others can access the artifacts by adding a dependency to the pom.xml file in a Maven project.

No comments:

Post a Comment