Skip to content

Beginner guide to Maven Central publishing

Posted on:May 4, 2020

Maven Central is the central repository for Maven, the main Java repository where you can find libraries and components. Java developers are using this repository to create java application and manage efficiently dependencies.

[!NOTE] Introduction I use to be very intimidated by pushing an artifact (i.e. a java package) to Maven Central, as it seems very complicated to me by the past. For example push to npmjs seems more easy (npm publish !).

This article is just a guide (at least for myself) on how to push easily some content to Maven Central, with just a Github account.

Before starting why pushing to Central ?

Maybe you are asking yourself why pushing to Maven Central when we have in 2020 multiple way of releasing java package ?

First step, create account and get a groupId

The issue must describe the project you want to share. The most important information in the case is the groupId. The groupId is the main identification of you as a package provider, so it must be unique. It is generally an url in reverse order like : to.dev.

For a hobbyist developer using GitHub or equivalent git platform I recommend the following : io.github.YOURUSERNAME.

Once the ticket is created, you will have to prove that you own the groupId provided (for GitHub it’s just a matter of create a new temporary project repo)

Second step, make your project pretty

Here is the small maven project I want to share on Central:

https://github.com/wiiisdom/biar-manager

It’s a very simple library that is designed to read a specific file format.

There is a long list of requirement for your project to be accepted on OSSRH, you can read it here.

To resume the requirements:

For reference here his my pom.xml file that fit the requirements.

Finally deploy !

Do not forget to install gpg and create a new key if you don’t have one (I found again my key generated in 2004 yes I am old !)

brew install gpg # mac example
gpg --gen-key

Using Maven it is needed to use the right distributionManagement and help yourself by using the nexus-staging-maven-plugin plugin:

<distributionManagement>
  <snapshotRepository>
    <id>ossrh</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  </snapshotRepository>
</distributionManagement>
<build>
  <plugins>
    <plugin>
      <groupId>org.sonatype.plugins</groupId>
      <artifactId>nexus-staging-maven-plugin</artifactId>
      <version>1.6.7</version>
      <extensions>true</extensions>
      <configuration>
        <serverId>ossrh</serverId>
        <nexusUrl>https://oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

(again this is available in my pom.xml)

You must auth on the OSSRH repository by modifying your settings.xml file under ~/.m2:

<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>your-jira-id</username>
      <password>your-jira-pwd</password>
    </server>
  </servers>
</settings>

Use the id and password you have created at the start of this article.

And then if your maven project have already the right version you can directly push to OSSRH using:

mvn clean deploy

The property autoReleaseAfterClose of nexus-staging-maven-plugin set to true will directly push the artifact from snapshot to the release repository.

Note: to change your Maven project version you can use the following:

mvn versions:set -DnewVersion=1.2.3 # set the version
mvn versions:commit # remove the pomBackup file
mvn versions:revert # back to previous version

After a few hours you will be able to see your package on Maven Search

Nice job !

Other stuff to speak of

During my exploration I have found this tool : https://jitpack.io/

It seems to be an easy way to consume an existing git repository as maven dependency. I have not yet tested it but it feels like the flexible I miss with Maven (Composer for PHP allow to consume git repo, npmjs allow to consume git repo, etc…)

References

Conclusion

Well there is more steps than npmjs but it is very affordable and you can do the same. Maybe it can be also the time for me to explore Gradle or other new tooling around Java… Let me know if you have already pushed an artifact to Maven Central ? Thanks for reading !