Skip to main content
blog title image

8 minute read - Java For Testers Maven

How to create and release a jar to maven central

Oct 21, 2016

TLDR; The instructions on apache and sonatype site are pretty good to help get library released to maven central. But you’ll need to learn about pgp signing and might need more rigour in your pom file. A fairly painless learning process that I recommend you go through and release something to the world.

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, that way 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.

Now that I have a groupid that will synchronise 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.

I decided not to include the full version of Selenium WebDriver - which I normally do when I use it:

i.e.

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

Instead I wanted the minimum I could add, since 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>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </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

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 group id the wrong way round

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
href=http://pool.sks-keyservers.net:11371/&gt;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 i.e.

  • 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

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 ci 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 were very helpful in getting me setup, I was very impressed given that the oss staging repositories and syncing to maven central is a free service.

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

References