DEV Community

Cover image for How to Publish a Java Artifact Built with Gradle to a Nexus Repository part 1
Otobong Edoho
Otobong Edoho

Posted on

How to Publish a Java Artifact Built with Gradle to a Nexus Repository part 1

What is a Nexus repository?

Nexus repository is a publicly accessible repository manager from Sonatype that organizes, stores and helps in distribution of artifacts used in software development.

It has built in support many for artifact formats such as Docker, Java, Go, PHP, Python etc.

Artifacts simply put refers to outcome of software build and packaging process

Importance of Publishing Java Artifacts to a Central Repository

Publishing Java artifacts to a central repository, like Nexus, is an important practice in modern software development for several reasons:

  1. Team Collaboration: In a team environment, a central repository allows developers to share libraries, frameworks, or any reusable code easily.

  2. Integration with CI/CD Pipelines: Publishing to a central repository is often a key step in CI/CD pipelines. Artifacts can be automatically deployed to a repository after a successful build, making them immediately available for testing or production deployment.

  3. Version Control: Central repositories allow developers to manage multiple versions of an artifact. This enables the use of specific versions, ensuring compatibility and stability across different project development.

Prerequisites

  • Java Development Kit 17 installed.
  • Gradle installed and configured.
  • Access to a Nexus Repository (public or private).
  • Basic understanding of Gradle build scripts (build.gradle).
  • An EC2 Instance or any other Cloud Service

Install Nexus

So before we install nexus we will setup an EC2 instance where we can install and run nexus.

This is an EC2 instance

To install Java on the instance

apt install openjdk-17-jre-headless

When that is done you can install nexus using this command

cd /opt
wget https://download.sonatype.com/nexus/3/nexus-3.71.0-06-unix.tar.gz
ls
Enter fullscreen mode Exit fullscreen mode

It comes as a tar ball so we need to extract it

tar -xvzf nexus-3.71.0-06-unix.tar.gz

Run ls and you should see this

terminal

Run ls -l to see the files and the owners, the root has ownership to the files so we need to give ownership to a nexus user which we will create. It is best practice to always create a user for any service we want to run.

Let's create a nexus user and add the user to nexus group

adduser nexus
usermod -aG nexus nexus
Enter fullscreen mode Exit fullscreen mode

while still in the /opt directory run this command

chown -R nexus:nexus nexus-3.71.0-06
chown -R nexus:nexus sonatype-work
Enter fullscreen mode Exit fullscreen mode

prompt terminal

Set nexus config so it will run as a nexus user
vim nexus-3.71.0-06/bin/nexus.rc

This should open up the file in vim edit the run_as_user="nexus" like this

Now switch to the nexus user and run the nexus service

su - nexus
/opt/nexus-3.71.0-06/bin/nexus start
Enter fullscreen mode Exit fullscreen mode

Use the command below to check the service and on which port running.
terminal netstat

We can see that nexus is running in port 8081, if the port is blocked on your EC2 instance you need to configure the security groups to allow inbound traffic on that port.

Next Use the public IP address of your instance and the nexus port number like this
18.232.173.21:8081
to be able to access the nexus interface from our browser

nexus interface

To get the default password for the admin interface navigate to the following directory

/opt/sonatype-work/nexus-3.71.0-06/
cat admin.password
Enter fullscreen mode Exit fullscreen mode

Use that to login, the username is admin

After logging in the admin interface should look like this

repository nexus

  • maven-releases repository is where we store artifacts that have been tested and are ready to be deployed to production.
  • maven-snapshots repository is where we store artifacts that are still in the development and test phase.

Let's say a developer in our company want to publish an artifact to nexus we can't let them use the admin account, we must create an account for that user

Navigate to the Users tab and create local user

Nexus users tab

Nexus user form

Go to User roles and create a role for the created user

User role

Best Practice: We don't want our user having too many privileges so we will only assign a role in which the user needs to carry out a task

role form

We are building with gradle in this case but the role still works the same as maven so we will set the following role

set role

Now we Assign that role that we created to the user

assign role

Configure Gradle with Nexus
Apply the following configurations and plugin to the build.gradle file

group = 'com.example'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '17'
targetCompatibility = '17'

apply plugin: 'maven-publish'
Enter fullscreen mode Exit fullscreen mode

This code block publishes the artifact that we have built

publishing {
//  The Artifacts we are going to upload
    publications {
        maven(MavenPublication) {
            artifact("build/libs/java-react-example-${version}.jar") {
                extension 'jar'
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

This code block defines the address that we are going to publish to.
The allowInsecureProtocol = true
Allows the publishing to work regardless of the fact that we are not using https
The credentials declares the user account that we created on our Nexus repository, so it will give access for our user to publish to the repository

// The Nexus repo that we will upload the Jar file to
    repositories {
        maven {
            name 'nexus'
            url "http://[Your Public IP]:8081/repository/maven-snapshots/"
//        So that it will all HTTP
            allowInsecureProtocol = true
            credentials {
                username project.repoUser
                password project.repoPassword
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's create a gradle.properties in our project file to store our user secret

repoUser = oty
repoPassword = xxxxxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Create a settings.gradle file if it doesn't exist and add the project name

rootProject.name ='java-react-example'
Enter fullscreen mode Exit fullscreen mode

gradle settings

If you haven't already built the project run then publish it

gradle build
gradle publish
Enter fullscreen mode Exit fullscreen mode

gradle build

build jar

gradle publish

Now we can go to our nexus repository and view our publish artifact

repo publish

Happy Reading!!!
Please Like and Follow if You liked the Article.
Thank You.

Top comments (0)