Publish java library to JFrog Bintray and OSS Sonatype with Gradle

Aliaksandr Rasolka
3 min readMar 7, 2019

--

Few steps but so much pain in the process…

Photo by Casey Horner on Unsplash

Checklist:

  1. Create Bintray account
  2. Modify build script
  3. Publish to local repository and validate publication
  4. Enable automatic signing on upload on Bintray repository
  5. Publish artifact to Bintray
  6. Synchronize publication with JCenter repository
  7. Check availability
  8. Create an account on Sonatype JIRA
  9. Create ticket and wait until ticket will be resolved with “Central sync is activated for <your space>” reason
  10. Wait for 4–5 hour or even better for 1 day
  11. Synchronize publication with Maven Central repository
  12. Check availability
  13. Cheers!

My story was started right after completion of small library. Some time after I ask myself — how do I distribute this library?

Based on my previous experience I prefer to JitPack because this way simple as much as possible.

Just find your library, modify you build script and use it. The problem is — additional repository in your build script. Not all people want to resolve one more additional repository in a target project.

The second problem — I should somehow care about versioning. This case I can only associate artifact version with branch, commit, or release.

As result I got a Gradle build script looks like:

repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.rosolko:wdm4j:master-SNAPSHOT'
}

Next I’m trying to use a JFrog Bintray to publish artifacts (bonus of this option — ability to sync artifacts with maven central repository). Registration with my GitHub account allows me to create a free account for open source using. Right after — create a repository, let’s call it maven with Maven type. For now — that’s it.

This case we need to modify our build script, so — let’s do it!

First of all we need to add maven-publish plugin and define several fields to successfully publish valid artifacts. Deep documentation you can find in Gradle publish documentation.

apply plugin: "maven-publish"

Next register tasks that build and collect artifacts:

tasks.register("sourcesJar", Jar) {
from sourceSets.main.allJava
classifier "sources"
}

tasks.register("javadocJar", Jar) {
from javadoc
classifier "javadoc"
}

Next to we need to create a publishing:

publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom {
name = "wdm4j"
description = "WebDriver binary manager for java"
url = "https://github.com/rosolko/wdm4j"
licenses {
license {
name = "MIT"
url = "https://github.com/rosolko/wdm4j/blob/master/LICENSE"
}
}
developers {
developer {
id = "rosolko"
name = "Aliaksandr Rasolka"
email = "rosolko@gmail.com"
}
}
scm {
connection = "scm:git:https://github.com/rosolko/wdm4j.git"
developerConnection = "scm:git:https://github.com/rosolko/wdm4j.git"
url = "https://github.com/rosolko/wdm4j"
}
}
}
}
}

Right after that we configure bintray:

bintray {
user = project.hasProperty('bintrayUser')
key = project.hasProperty('bintrayApiKey')
publications = ['mavenJava']
pkg {
repo = 'maven'
name = 'wdm4j'
vcsUrl = 'https://github.com/rosolko/wdm4j.git'
licenses = ['MIT']
}
}

After that we can execute publishToMavenLocal task, check that we have valid artifacts, and perform bintrayUpload task to upload.

As result library should appear in previously created repository. And of cource we receive an ability to link library with jcenter.

After some indexing time it will appear and available for use:

repositories {
jcenter
}
dependencies {
implementation 'com.github.rosolko:wdm4j:1.0.0'
}

On this step we can stop, but bintray has an ability to sync artifacts with maven central repository, why not. Let’s set this up.

On this step problems will starts. For first you can’t easily create account and publish something.

For this sonatype have a special guide, short list:

  1. Create JIRA account for sonatype
  2. Open an issue to create your space for library upload
  3. Sign your artifacts
  4. Upload

If with first two points there are no problem — signing can be quite tricky, but bintray allows you to configure repository with automatic GPG signing uploaded files using Bintray’s public/private key pair.

Right after that upload your artifacts again and sync library repository with Maven Central.

For this operation you also need key and token. Sync, release and repository when sync done and wait.

Note: The responsiveness of the Maven Central gateway at Sonatype fluctuates. This operation may take several minutes depending on the weather.

As result you library will appear on maven search portal and you can use it in your build script without modifying your existing repository list:

repositories {
mavenCentral()
}
dependencies {
implementation 'com.github.rosolko:wdm4j:1.0.0'
}

Yay, quest complete, you are awesome ~(* — *)~

Complete build scripts and library itself you can easily find on GitHub:

--

--