Prasham Trivedi

Using Bitbucket As Private Maven Repository

October 12, 2018

2 minutes to read.

Ecosystems like Android, Kotlin and Web are widely adopted because they are openly available to use and study. Big platforms and small libraries are both open sourced in Github or other places like BitBucket or Gitlab. And the code is easily distributed, whether you want to deal with technicalities of JCenter or Maven or using simplicty of Jitpack. Making code open and distributing to the world is one of the most easier tasks for a library developer.

But there can be a time & use case where you have to create a Java library which should not be roaming open. It’s either an SDK which is used for internal use or of a Properitory product which can only be used by authorized people. That means neither your code nor your binaries can be shared openly with world. And in this situation there aren’t much options to begin with. And whatever options are there, they are either paid or requires some sort of maintenance from our end. In case we don’t have any time/resource for any, the other option is using Private Git repositories for hosting and distributing maven artifacts. Here is how we can use Bitbucket as our private Maven Repository.

Creating maven repository consists of two steps. 1. Uploading code to maven repository and 2. Downloading code to maven repository. And for both the things we will be using Git and Gradle.

1. Uploading.

To upload a code using Gradle script. Gradle has many plugins. And for our task, we will be using maven plugin.

To use Git from Gradle we will use wagon-git Plugin. There are couple of good articles that will help us on how to use Wagon Git for uploading maven repository on Bitbucket. And they are good to follow. However there are couple of things where this post differs from the posts mentioned here.

Mainly, these posts were written around 2015. At the time of writing they are around three years old. And in these three years, Apis of Bitbucket are changed. They were written and tested against Bitbucket api 1.0. This version is deprecated at the time of writing and will be removed in December 2018. This change does not affect uploading part in these post and hopefully Wagon Git will keep continue for forseeble future as all it does is to run Git Commands to upload the files. The big change is in Downloading, and we will conver the change in next section.

However, the posts above assume we are using SSH for checking out the repositories and the scripts were written in the post according to the assumption. But not every developer is that comfortable or aware using SSH. And that’s where our post differs in Uploading. If you are using ssh, uploading script mentioned in above posts will work as it is. If you are using normal authentication, read on…

The only change you should make is to change your repository URL from git@bitbucket.org to https://bitbucket.org. Your updated script should be.

This was the smallest change we have to do in whole process. At this point your maven repository should be uploaded in release branch(Or Snapshot branch, based on what you run). Now let’s move to second part. Fetching a privately uploaded library.

2. Downloading.

This is where fetching process changes compared to the posts mentioned elsewhere in internet. In those post they were using Bitbucket 1.0’s raw endpoint. And the main problem is, raw endpoint is not available in 2.0 version of APIs. The means are changed here, but the principle is not. All we have to do is to get raw file url of maven root which was hosted on BitBucket.

And the trick here is simple. When you browse a BitBucket repository, it generates a url with src in it. Replace src in that URL with raw and we will get Raw url of the repository. After that we need to provide credentials to access files from that repository.

To pass those credentials. Follow steps below.

  • In root level build.gradle, go to repositories section in allProjects.
  • Create credentials{} DSL there.
  • Pass raw url of the repository.
  • Pass your username and password as it is
  • Add authentication{} DSL
  • Mostly we are providing Basic Authentication method (i.e. Simple username and password), so we need to pass basic(BasicAuthentication) as an arguments in authentication DSL.

After all these, your gradle code would be

comments powered by Disqus