simple library publishing with Gradle

So, you switched to Gradle and just finished you new shiny Android library (or
java library). And of course you want to share it with the community.

Here’s a quick guide for those who never published their libraries before.

What is maven central?

In java world libraries are usually stored in a big repository called Maven
Central. You may think of it as of the NPM repository for Node.js or as of
PyPI for Python.

It lets you quickly find a library at
http://search.maven.org. It is also used by other
tools like Gradle, Please. Finally, gradle
uses this repo if you included mavenCentral() into your repositories section.

Registration

First, you will have to register. No, don’t try to find a ‘Register’ link on
the Maven Central website. Instead, you should register on the Sonatype
website.

What is Sonatype? They are the guys who created Maven back in 2002. But don’t
try to find a registration link on their site either, instead go to their issue
tracker. (well-known
JIRA) and register there:

In the issue details fill in your library description and summary, specify
project URL if any and version control URL (like bitbucket or github link).
Ensure you have a valid group id. It’s should be somehow related to your
package and should contain at least one dot in it. So, names like ‘johndoe’ are
invalid, but ‘com.johndoe’ are ok if you own the ‘johndoe.com’ website. If you
don’t own any website - you may use your version control hosting name, e.g.
com.bitbucket.johndoe or com.github.johndoe.

Then - relax and wait. It may take up to 2 business days to process your ticket
manually.

After that you will receive a confirmation that your configuration has been
prepared and you can publish your library.

Gradle setup

There are many possible ways of how to set up your gradle for maven publishing.
I used a file I found in some other repository and I like it’s separated from
the main build.gradle.

Pushing the archives

Now you can do:

$ gradle uploadArchives

Your library will be built, signed and pushed to the sonatype repo. Please,
ensure that ‘signing’ stage was not skipped. It is skipped if your library name
is ending with “-SNAPSHOT”, but for releases signing is mandatory.

If all went fine - go to the OSSRH web
UI and look for your ‘staging’
library. It should be somewhere at the end of the list. Select it, and press
‘Close’ button. Closing a library actually means that you’re ready to release
it. Another option is ‘dropping’ a library, which means removing it from the
list. If closing went fine - you should see a ‘Release’ button active. Press it
and then get back to JIRA and post a comment there that you promoted you
library.

After that you should get a response from Sonatype that your library will be
available in ~10 minutes and it will be synced with the Maven Central in the
next few hours.

To me that meant that I could use my library in other projects in a few minutes
after I released it (e.g. using in gradle like ‘compile groupID:name:version’),
but the search.maven.org found it only next morning.

So, that’s it, and if you still have any problems - ask Sonatype support, I
found them to be very helpful. Happy coding!