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. The code is easily distributed, whether you want to deal with technicalities of
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 and in a way it is giving back to the community from where we have learned a lot.
But there can be a time & use case where you have to create a Java library which should not be public. It’s either an SDK which is used for internal use or of a properitory product which can only be used by authorized developers. That means neither your code nor your binaries can be shared publicly. In this situation there aren’t much options to begin with, 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 of the above, 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.
- Uploading code to Maven repository
- Downloading code from Maven repository, for both the things we will be using
To upload a code using Gradle script. Gradle has many plugins. For our task, we will be using
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. These options 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 were around three years old. In those three years, Apis of Bitbucket have 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 Gitwill 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. 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
https://bitbucket.org. Your updated script should be.
apply plugin: 'maven'
compile fileTree(dir: 'libs', include: ['*.jar'])
configuration = configurations.archives
pom.groupId = GROUP
pom.artifactId = POM_ARTIFACT_ID
pom.version = VERSION_NAME
configuration = configurations.deployerJar
This was the smallest change we have to do in whole process. At this point your maven repository should be uploaded in
Snapshot branch, based on what you run). Now let’s move to second part. Fetching a privately uploaded library.
This is where fetching process changes compared to the posts mentioned elsewhere on the internet. In those post they were using Bitbucket 1.0’s
raw endpoint. Now 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.
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
rawand 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
- Pass raw url of the repository.
- Pass your username and password as it is
- Mostly we are providing Basic Authentication method (i.e. Simple username and password), so we need to pass
basic(BasicAuthentication)as an arguments in
- If you are using other authentication methods, Refer to gradle documentation here and change this as per your needs.
After all these, your gradle code would be