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:
- Start on “maven guide central repository upload” page
- Learn the need for PGP signing.
- Learn the basic format for POM.
- Read the up-to-date POM documentation linked to
- Set up POM
- coordinates, name, description, URL, license, developer, SCM.
- Find the link to the “Open Source Software Repository Hosting (OSSRH)” Guide
- Follow the “Open Source Software Repository Hosting (OSSRH)” Guide.
- follow steps to create jira account and ticket to access OSSRH.
- Read the Maven deploy guide
distributionManagementsection in the POM.
- Edit the
.m2\settings.xmlfile to include the OSSRH details.
- Read this Sonatype blog post on how to configure GPG signing:
- Read the full instructions on Sonatype
- I downloaded 2.3.3.
- 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.
mvn clean deployto create a staging release.
- Make sure you get the groupid correct.
- I had
co.uk.compendiumdevas groupid instead of
uk.co.compendiumdevso 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.
- "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. Successfully deployed SNAPSHOT versions will be found in https://oss.sonatype.org/content/repositories/snapshots"
- 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.
- For example, https://github.com/eviltester/selenium-driver-manager-example
- The above project uses my library and has comments in the
pom.xmland the repository configuration for using the
-SNAPSHOTrelease on OSSHR staging.
- Create a full release.
- “Performing a Release Deployment.”
- Use “Performing a Release Deployment with the Maven Release Plugin”
- Rename to remove
mvn clean deploy
- Synchronize to Maven central
- Add a comment to my Jira ticket on Sonatype, and they enabled synchronization to Maven central.
- Wait an hour or so
- Project appears on maven central
- Amend my ‘test’ project to use the version from Maven central
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:
<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
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:
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
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:
- And not
When it was available from the search, then I could
mvn clean deploy.
I still have a lot to learn here.
As a beginner:
- I’ve added a lot of ‘stuff’ to the
pom.xmlthat 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.
- The Jira that I used during the setup process
- My example project
- I ‘cribbed’ from another GitHub library that was using WebDriver for tips