How to publish your library to Maven

Signing and deploying your Gradle artifacts to Maven Central

Nishant Aanjaney Jalan
CodeX
3 min readJul 21, 2024

--

A couple of months ago, I was researching how to publish a library to Maven Central. It was quite a headache since many tutorials needed to be clearer. The Gradle documentation could have helped more. But in the end, I deployed my Kotlin package to the maven registry successfully. This is a tutorial for those who are finding it troublesome to do the same.

Photo by DESIGNECOLOGIST on Unsplash

Prepare your project

It is a good idea to have your project as a separate Gradle module. It helps to keep your code separate from each other. Additionally, using your library from another module (like :sample or :preview) in the same project is a great way to check if your module works as intended and sets a great example for your users to look at how to use your library. In this case, we will look at how to publish the lib module.

Example project structure:

.
├── lib
│ ├── build.gradle.kts
│ └── src
│ ├── main
│ └── test
├── gradle
│ ├── libs.versions.toml
│ └── wrapper
├── preview
│ ├── build.gradle.kts
│ └── src
│ ├── main
│ └── test
├── build.gradle.kts
├── gradlew
├── gradlew.bat
└── settings.gradle.kts

Prerequisites

Create an Account with an OSSRH

Before you can publish anything, you need an account with any OSSRH (Open Source Software Repository Hosting) service. For my project, I created an account with SonaType Central. This is well documented here.

Note: central.sonatype.com and central.sonatype.org are two different websites but are somehow interlinked. I was quite confused at first about the information they had in both these websites.

Create a namespace

A namespace is similar to your groupID portion of a dependency URL. I signed up to Central Sonatype with my GitHub account and I had a free namespace of io.github.cybercoder-naj. I am not sure if you get any free namespaces if you sign up with the other providers (Let me know!). Meanwhile, have a look at this website to understand how to register and verify your custom namespace.

Registering your GPG Keys

When publishing your artifacts, you not only send the jar files but also their signature. Central Sonatype will reject any artifacts that were uploaded with missing or invalid signatures.

But why do we need to sign the artifacts we publish? I have just the right article if you are interested:

If you already understand why your signature matters, then you should be able to create your public-private key pair. Here is a cheat sheet to remind you of the important commands for handling your GPG keys:

Building and publishing your artifacts

Documentation from Gradle, Central Sonatype and JetBrains did not get me far in trying to upload artifacts on MavenCentral. I found Niklas Baudy’s gradle plugin the most intuitive plugin with easy-to-follow steps that deliver the results. Be sure to check out their brilliant documentation here.

In brief, with the help of their documentation, you should:

  1. Configure the type of library being published.
  2. Configure the MavenCentral URL. Do not forget to invoke signAllPublications() method.
  3. Setup your repository details (the POM).
  4. Set your environment secrets. You will have to export your private key in this step.
  5. The plugin provides different publishing options. I use ./gradlew :lib:publish.

If you wish to deploy your library with a CI/CD pipeline, here is an example GitHub Actions yaml file.

Conclusion

I had published a couple of TypeScript libraries on NPM.JS. They were quite straightforward to work with. The first time publishing a Kotlin library took me more than a day. I hope this article helps you first-time publishers have a better experience showcasing your creations among the community.

If you are curious, the article references this project currently under development.

Thank you for reading if you made it this far. If you liked my content, consider following and applauding my article. Cheers!

Want to connect?

GitHub profile
Portfolio website

--

--

Nishant Aanjaney Jalan
CodeX

SWE Intern@JetBrains | UG Student | CS Tutor | Android & Web Developer | OCJP 8 | https://cybercoder-naj.github.io