Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Create and Release a JAR to Maven Central

DZone's Guide to

How to Create and Release a JAR to Maven Central

The instructions on Apache and Sonatype's sites are pretty good at helping get a library released to Maven central. But it can be a bit painful. Read on to find out more.

· Java Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

I spend most of my time with Java writing stand-alone applications to support testing or code that we run as part of CI. I haven’t had to create a library that I make accessible to other people through Maven central.

I thought it was about time I did so.

In this post, I’ll describe what I did and how I got a .jar in Maven Central.

What Is the Library?

As part of my Selenium WebDriver online training course, I created a ‘driver manager’ to allow code to more easily run across different browsers. It works fine for the context of my course. Over time, I’ve started splitting the course source into multiple parts:

And I’ve had to copy the Driver.java into the continuous integration project.

I decided to pull it out into a separate library and make it accessible via Maven central, so it will be easier for people taking the course to use the Driver class in their own code.

And I can start maintaining it as a project on its own merits with better code and better flexibility, rather than something that just supports the course.

Summary of What to Do

What follows is a ‘checklist’ created from my notes about how I released it:

Now that I have a groupid that will synchronize to maven central, it should be a simpler process if I want to create any future libraries.

A Bit More Detail

The documentation I linked to is pretty good. I mostly just copied the information from there.

And you can see the results in the released library code:

And the sample project that uses the library:

Changed My Code to Use Minimal Libraries

One change I made to the library POM.xml that is different from my normal use of the code in projects is that I decided not to include the full version of Selenium WebDriver — which I normally do when I use it:

For instance,

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-server</artifactId>
   <version>3.0.1</version>
</dependency>

Instead, I wanted the minimum I could add, as I know that the projects using it will be incorporating the full version of Selenium WebDriver.

 So I just used the Java Interface:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.0.1</version>
</dependency>

Configuring Repositories in the POM.xml

I haven’t had to do this for a long time. I vaguely remember doing this in the past as a workaround for some local issue we had.

In order to access the -SNAPSHOT release version of the library, I have to have the repository configured in my POM.xml:

<!-- to use snapshot versionsof the driver manager we need to use the OSS nexus repo -->

<repositories>
    <repository>
        <id>osshr</id>
        <name>OSSHR Staging</name>
        https://oss.sonatype.org/content/repositories/snapshots
    </repository>
</repositories>

I imagine that this might prove a useful workaround if I ever encounter a site that has configured the Maven config via settings that we are unable to access easily.

Deploy Was Easier Than I Thought

I haven’t used the release deploy in Maven before. And the instructions had a whole bunch of commands:

//perform a release deployment to OSSRH with

mvn release:clean release:prepare

//by answering the prompts for versions and tags, followed by

mvn release:perform


But in the end, I didn’t have to do this. I changed the version to remove -SNAPSHOT and it ‘released’ when I did a mvn clean deploy.

Tagging a Release on GitHub

 I haven’t ‘released’ on Github before, so I created a release via the GitHub GUI on the releases page:

Gotchas

What went wrong?

I Tried to Use a Groupid That I Don’t Own

I’ve been pretty laissez-faire with my groupids in my projects and high-level package names because I’ve never released one before. But to use Maven central you need to have a domain that you own. And someone has snapped up the .com that I often use in my code, so I needed to use the .co.uk that I own.

I might well start changing the code that I create to use this new groupid now.

I Put My Groupid the Wrong Way Around

 I tried  mvn clean deploy for a snapshot release and I received:

[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:
1.6.7:deploy (injected-nexus-deploy) on project selenium-driver-manager:
Failed to deploy artifacts: Could not transfer artifact co.uk.compendiumdev
:selenium-driver-manager:jar.asc:javadoc:3.0.1-20161020.083347-1 from/to
ossrh (https://oss.sonatype.org/content/repositories/snapshots):
Access denied to: https://oss.sonatype.org/content/repositories/snapshots/co/uk/
compendiumdev/selenium-driver-manager/3.0.1-SNAPSHOT/
selenium-driver-manager-3.0.1-20161020.083347-1-javadoc.jar.asc,
ReasonPhrase: Forbidden. -> [Help 1]

I checked that my credentials were correct by logging into the OSS nexus system. My issue was that instead of using groupid (uk.co.compendiumdev), I mistakenly used co.uk.compendiumdev.

So don’t do that.

I Forgot to Release the GPG key

I forgot to release the GPG key when I created it, so I ended up trying to do a final release and seeing the following error during mvn clean deploy:

[ERROR]     * No public key: Key with id: (xxxxxxxxxxxxx) was not
able to be located on
http://pool.sks-keyservers.net:11371/.
Upload your public key and try the operation again.


Make sure you do this early in the process.  Also, I had to wait 10-20 minutes before it was accessible. To check, visit the site you uploaded to and then search for the key. I had to search for the key id with 0x in front of it:

  • 0x740585A4
  • And not 740585A4

http://pool.sks-keyservers.net/pks/lookup?search=0x740585A4

When it was available from the search, then I could mvn clean deploy.

Future Work

 I still have a lot to learn here.

 As a beginner:

  • I’ve added a lot of ‘stuff’ to the pom.xml that I don’t fully understand and need to research.
  • I’m sure I’m taggging the release on GitHub inefficiently.
  • I’ve only done one release so I’m not sure if it is fully setup yet.
  • I do this manually and haven’t added a CI deploy — when I do I’ll read the Sonatype blog post more carefully.

 And I have an actual todo list:

  • I need to document the library and example in more detail if I want usage to spread beyond my course.
  • I need to amend my WDCI and course code to use the library.

But it was a lot less daunting than I expected, and the documentation was pretty clear, and the OSSHR team was very helpful in getting me set up. I was very impressed given that the OSS staging repositories and syncing to maven central is a free service.

I hope that helps someone on the road to making their release. All in all, it was a good learning experience.

References

Moving towards a private or Hybrid cloud infrastructure model? Get started with our OpenStack Deployment Models guide to learn the proper deployment model for your organization.

Topics:
repositories ,maven ,jar ,java

Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}