Publish your gradle library using Sonatype Nexus3

WS Ayan
2 min readJan 1, 2021

If you want to publish your library from your own server this is the way. Sonatype Nexus3 offers a free repository manager which allows you to publish your library in your own server. You can publish your built module manually or automate publish by following the steps.

Prerequisite

  1. OS: Ubuntu 20.04.1 LTS
  2. Docker version 19.03.13

Pull sonatype/nexus3 repo manager:

$ docker pull sonatype/nexus3

Running

To run, binding the exposed port 8081 to the host, use:

$ docker run -d -p 8081:8081 --name nexus sonatype/nexus3

When stopping, be sure to allow sufficient time for the databases to fully shut down.

docker stop --time=120 <CONTAINER_NAME>

To test:

$ curl http://localhost:8081/

Signing in

Open bash in docker and find admin password:

$ docker exec -it containerID /bin/bash 
cd /nexus-data
cat admin.password

Open http://localhost:8081/ in browser. Now sign in to sonatype and change password.

Upload library to repository manager

Add this in project roots build.gradle:

maven { 
url "http://localhost:8081/repository/maven-public/"
}

Add this in project libs build.gradle:

uploadArchives { 
repositories {
mavenDeployer {
repository(url: "http://localhost:8081/repository/maven-snapshots") {
authentication(userName: "admin", password: "your-password")
}
pom.version = "1.0-SNAPSHOT"
pom.artifactId = "libraryname"
pom.groupId = "com.example"
}
}
}

Run following command to upload:

./gradlew uploadarchives

Library will be uploaded in localhoast:8081 Go to localhoast:8081 and browse maven-public directory. You will see your library is uploaded. Generate gradle implementation by selecting the artificate.

Using the published library

While using the library don’t forget to add it on project roots build.gradle

maven { 
url "http://localhost:8081/repository/maven-public/"
}

Now you can use the library from apps build.gradle

implementation 'com.example:libraryname:1.0.0'

Support links

dockerhub

github

--

--