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:
- getting started project
- in addition to the main code on the course
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.
- start on “maven guide central repository upload” page
- https://maven.apache.org/guides/mini/guide-central-repository-upload.html
- learn the need for PGP signing
- learn basic format for pom
- read the up to date pom documentation linked to
- https://central.sonatype.org/publish/requirements/
- set up pom
- coordinates, name, description, url, license, developer, scm
- find link to the “Open Source Software Repository Hosting (OSSRH)” Guide
- follow the “Open Source Software Repository Hosting (OSSRH)” Guide
- http://central.sonatype.org/pages/ossrh-guide.html
- follow steps to create jira account and ticket to access OSSRH
- read the maven deploy guide
- http://central.sonatype.org/pages/apache-maven.html
- add
distributionManagement
section in the pom - edit the
.m2settings.xml
file to include the OSSRH details
- read this sonatype blog post on how to configure GPG signing
- https://central.sonatype.org/publish/requirements/gpg/
- read the full instructions on sonatype
- https://www.gnupg.org/download/index.en.html
- I downloaded 2.3.3
gpg --gen-key
- RSA and RSA
- 2048 bits long
- set expiry
- upload your public key
- this is mentioned on http://central.sonatype.org/pages/working-with-pgp-signatures.html
- NOTE: make sure you do this. I forgot and my final deploy failed. Do this now and all will go swimmingly.
- use
mvn clean deploy
to create a staging release- make sure you get the groupid correct
- I had
co.uk.compendiumdev
as groupid instead ofuk.co.compendiumdev
so it didn’t deploy to staging - I saw other Jira tickets with similar problems so it is a common mistake
- I had
- make sure you get the groupid correct
- use staging release in a project
- http://central.sonatype.org/pages/apache-maven.html
- “SNAPSHOT versions are not synchronized to the Central Repository. If you wish your users to consume your SNAPSHOT versions, they would need to add the snapshot repository to their Nexus Repository Manager, settings.xml, or pom.xml.”
- see this guide on how to add a repository into your pom.xml https://maven.apache.org/guides/mini/guide-multiple-repositories.html
- create a test project using staging
- e.g. https://github.com/eviltester/seleniumWebDriverCompendium/tree/main/selenium-driver-manager-example
- above project uses my library and has comments in the
pom.xml
and the repository configuration for using the-SNAPSHOT
release on OSSHR staging
- create a full release
- http://central.sonatype.org/pages/apache-maven.html
- “Performing a Release Deployment”
- use “Performing a Release Deployment with the Maven Release Plugin”
- rename to remove
-SNAPSHOT
mvn clean deploy
- synchronise to maven central
- add comment to my Jira ticket on sonatype and they enabled synchronisation to maven central
- wait an hour or so
- project appears on maven central
- amend my ’test’ project to use the version from maven central
- Done
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/>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
- https://maven.apache.org/guides/mini/guide-central-repository-upload.html
- http://central.sonatype.org/pages/requirements.html
- http://central.sonatype.org/pages/ossrh-guide.html
- https://maven.apache.org/settings.html
- https://www.gnupg.org/download/index.en.html
- https://maven.apache.org/guides/mini/guide-multiple-repositories.html
- http://central.sonatype.org/pages/apache-maven.html
- http://central.sonatype.org/pages/working-with-pgp-signatures.html
- The Jira that I used during the setup process
- My example project
- I ‘cribbed’ from another github library that was using WebDriver for tips