How to set up private maven repository with artifactory

By | November 24, 2018

If you are an Intermediate or advanced android developers, you are now familiar with android modules. The number modules available is increasing each day exponentially. If you are planning to launch your own project, you might need to manage those modules in best and sustainable way.

How private maven repository helps to grow fast?

Well, we could now set up public maven custom maven repositories with GitHub and JitPack. However, if the number of projects you work on is quite large, and inside there’s lots of modules related with authentication, then definitely, you should consider setting up a private maven repository.

First you need a dedicated server of your own. Or you can purchase any vpn server from reputable companies. In this tutorial we will discuss about Artifactory. However, there’s other competitors that you might want to check out (1) Nexus, (2) Archiva.

Technology Advanced Programming - Banner

Artifactory Installation

We will set up the whole part in three small steps:

(1) Install Java: In this step we will install the Java Runtime

(2) Install Artifactory: We will download and install artifactroy

(3) Configure Artifactory – We will configure artifactory from web browser and set up necessary groups and permissions.

(4) Set up a test library project for android – We’ll add a repository to our server, than we will set up a new project with new module. And push this module to the server.

 

(1)  Install Java

SSH your VPS. And get the root privileges. Then perform the following commands:

# add-apt-repository ppa:webupd8team/java
# apt-get update
# apt-get install oracle-java8-installer

This will install java8. After installation you can check java: java –version.

(2) Install Artifactory

Download latest release from JFrog Open Source. Then upload this file to your server. You can use ftp or sftp (ssh ftp) to upload the file. Then run the following command to add the keys. Then with dpkg -i command install the deb file.

Add keys and install

$ gpg --keyserver pgpkeys.mit.edu --recv-key 6B219DCCD7639232
$ gpg -a --export 6B219DCCD7639232 | sudo apt-key add -
$ apt-get update
$ dpkg -i jfrog-artifactory-oss-4.5.1.deb

Start the artifactory

(3) Configure Artifactory

Open your browser and navigate to http://IPADDRESS:8081/artifactory/

Replace IPADRESS with the server ip address. You will then see the login screen. If its the first time you will need to set a new password for admin account.

(a) Next we will add a group. This group will connect users and permissions. Goto Admin>Groups. Click the new button to create a new permission. Lets name it ‘Contributors’. And lets describe it as ‘A group for library contributors’. We can skip the users part right now. Save it.

(b) Next we will create a permission. Browse to Admin>Security>Permissions. And click the New button at upper right. Lets name it ‘Contribute to Anything’ and check both ‘Any Local Repository’ and ‘Any Remote Repository’ box at the bottom. This will gain an access to any repository in the system to the contributors. If you don’t want to give access to all repository, leave those check boxes and select the repositories below to give access to.  Click the ‘Groups’ tab now. Now doube click the ‘contributors’ group, and check all the privileges you want to give. Lets check the ‘Delete/Overwrite’, ‘Deploy/Cache’, ‘Annotate’, ‘Read’ leaving the ‘Manage’ checkbox. Click save.

(c) Now create an user account. In the create section you can assign the user to a group. Remember the permission we set up for the group? Assigning this user to the contributors group will gain them relevant access. Click save, to save the user.

(4) Set up a test library for android

(a) Before logging out, lets create a new repository. Browse to Admin>Repositories>Local. Click the +New button to create a new repository. Lets name is ‘libs-release-local’. Click save.

(b) Log out and log into the new user account we have created. After login click on the username at top bottom. Clicking on it will require your password to unlock. Then in Authentication Settings copy and store the Encrypted Password somewhere. We will need it.

(c) Now in android studio, create a new project, with any name. Now add new android library module via File->New->New Module->Android Library. Lets make the package name as ‘my.group.package’.

(d) Now, Add this to root level build.gradle

buildscript {
dependencies {
// Add this line
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.0.1"
}
}

(e) Add this at the very first line of library’s build.gradle

apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'

(f) Add these at the end of the same file

def libraryGroupId = 'my.group.package'
def libraryArtifactId = 'thelibrary'
def libraryVersion = '1.0.0'

This will result the form of as my.group.package:thelibrary:1.0.0

(g)At these at the end of the same working file

publishing {
    publications {
        aar(MavenPublication) {
            groupId libraryGroupId
            version libraryVersion
            artifactId libraryArtifactId

            artifact("$buildDir/outputs/aar/${artifactId}-release.aar")
        }
    }
}
def artFile = file('artifactory.properties')
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(artFile))

def artifactory_username = versionProps['artifactory_username'].toString()
def artifactory_password = versionProps['artifactory_password'].toString()

artifactory {
    contextUrl = 'http://IPADDRESS:8081/artifactory'
    publish {
        repository {
            repoKey = 'libs-release-local'

            username = artifactory_username
            password = artifactory_password
        }
        defaults {
            publications('aar')
            publishArtifacts = true

            properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
            publishPom = true
        }
    }
}

(h) Now add a file named ‘artifactory.properties’ in the library directory. And paste the created user credentials here:

artifactory_username=User-Name
artifactory_password=Encrypted-Password

(i) Upload the library: User terminal to run the command

gradlew.bat assembleRelease artifactoryPublish

Use the library

Add this to app level build.gradle in the project that you want to use this library:

allprojects {
    repositories {
        jcenter()
        // Add these lines
        maven {
            url "http://IPADDRESS:8081/artifactory/libs-release-local"
            credentials {
                username = "${artifactory_username}"
                password = "${artifactory_password}"
            }
        }
    }
}

Add the dependancy

dependencies {
    compile ''my.group.package:thelibrary:1.0.0"
}

And We’re done!

Reference:

How to setup a Private Maven Repository for in-house Android libraries distribution