How to upload (Scala) jar to JCenter and MavenCentral with Gradle

2 minute read

This is a purely technical post, more like an instruction/tutorial.

Assume that you have built you project (written in Scala, but it will be relevant only for one point, see below) with gradle and now you want to upload jar, so that anyone can use it by maven/gradle/sbt/whatever – even ant, altough I used it with manually collected jars only.

It is well known that there are 2 main repos: mavenCentral and jcenter. They remind me Python 2 and Python 3, respectively: everyone says that Python 3 is better, but if you really want your lib to be used by everyone, use Python 2 – or, ideally, write “code that runs under both Python2 and 3” (so that you have to take worst from both worlds).

Fortunately, bintray team provides easy way to achieve both repos without much efforts.

Unfortunately, their original documentation describes evident cases, but omits what is really incomprehensible, e.g. they link to the start page of maven (however, in gradle plugin page they provide much more useful link). So I try to clarify it a little.

Brief how-to

  1. Go to OSS, register, create issue for your future artifact
  2. Go to bintray, register there and create repo (this is repo for artifacts, not your repo for code).
  3. Add code to build.gradle - see comments below or just copy-paste examples from pu4spark or atr4s. Also see how to hide credentials at stackoverflow answer.
  4. Run gradle bintrayUpload.
  5. Go to bintray again, press Sync with JCenter. Wait for some time (usually, hours).
  6. Sync with mavenCentral.

*Also take a look at this nice guide with a lot of screenshots; it is designed for Android, but the first part remains valid for any jars.

Brief comments to build.gradle

Note that bintray’s signing can be used for uploading to mavenCentral (if you are more lazy than paranoid).

MavenCentral doesn’t pass files without javadocs (see the almost-5-year-old issue) and they don’t care if you are using Scala. There are projects for generating javadocs, but it seems to be much simpler to just replace javadocs by scaladocs (see example).

Inclusion of mavenCentralSync section, which was in original guide, seems to be useless, because you will anyway need to manually sync package with jcenter first.

Also be careful with pom.xml generation: I didn’t research this bug, but if you move any of 3 append lines to pomConfig method, i.e. replace them by lines like the following:

description 'A library for Positive-Unlabeled Learning for Apache Spark MLlib (ml package)'

then gradle fails to add description tag to pom.xml for no reason! Even if we place something before description in pomConfig, it still won’t shown.

Leave a Comment

Your email address will not be published. Required fields are marked *