DEV Community

Cover image for A Complete Guide to Setting Up Nexus (2 Ways) + How to Connect Nexus to Jenkins
Jakub KoΕ›la
Jakub KoΕ›la

Posted on

A Complete Guide to Setting Up Nexus (2 Ways) + How to Connect Nexus to Jenkins

πŸš€ Setting Up Nexus Repository Manager


πŸ“¦ What is Nexus?

Nexus is an artifact repository, which helps to store artifacts like packages, compiled binaries, Maven files, etc. While developing software, it is a good idea to track such things; it helps to control access and deployment of every artifact in your software from a single location.


πŸ’‘ Why Integrate Nexus with Jenkins?

Manually uploading artifacts to Nexus is inefficient and prone to errors. Integrating Nexus with CI/CD pipelines like Jenkins automates this process, ensuring a seamless and reliable workflow.


πŸ“ Prerequisites

Before starting, ensure your system meets the following requirements:

  • RAM: Minimum 8GB
  • CPU: At least 4 cores
  • Java: OpenJDK 17 (or the version required by the latest Nexus release)

System requirements for Nexus depend on your needs and project size.
πŸ‘‰ Detailed system requirements: Nexus System Requirements


βš™οΈ Setting Up Nexus

This tutorial demonstrates two methods to set up Nexus on an Ubuntu virtual machine:

  1. As a systemd service
  2. Using Docker

πŸ› οΈ Method 1: Setting Up Nexus as Systemd Service

For this tutorial I used Java 17, but you should check what Java version is required right now here. Also you need to have sudo privileges.

Step 1: Install Java

# Update the Package index
sudo apt update

# Install OpenJDK 17
sudo apt install openjdk-17-jdk -y

# Verify the Installation
java -version
Enter fullscreen mode Exit fullscreen mode

Expected output:

azureuser@Nexus:~$ java -version
openjdk version "17.0.13" 2024-10-15
OpenJDK Runtime Environment (build 17.0.13+11-Ubuntu-2ubuntu124.04)
OpenJDK 64-Bit Server VM (build 17.0.13+11-Ubuntu-2ubuntu124.04, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

Step 2: Download and Install Nexus

Here, we will download the Nexus repository and create a specific user to run Nexus as a Systemd service securely. The current version download link is accessible here. For Linux, choose the Unix Archive.

1.Download Nexus

# Change to /opt Directory
cd /opt

# Download Nexus Artifact Repository
sudo wget https://download.sonatype.com/nexus/3/nexus-3.75.1-01-unix.tar.gz
Enter fullscreen mode Exit fullscreen mode

2.βž• (Optional) Verify MD5 Checksum

md5sum nexus-3.75.1-01-unix.tar.gz
Enter fullscreen mode Exit fullscreen mode

Compare the output with the checksum provided on the official site.

3.Extract and Rename Nexus

# Extract the downloaded file
sudo tar -zxvf nexus-3.75.1-01-unix.tar.gz 

# Rename for convenience
sudo mv nexus-3.75.1-01 nexus
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Nexus User

Now, as I mentioned earlier we will create a specific user which will be allowed to run Nexus as system service:

# Create a dedicated user for Nexus
sudo useradd nexus

# Set a password for the user
sudo passwd nexus
Enter fullscreen mode Exit fullscreen mode

Step 4: Add the User to sudoers

Add the Nexus user to the sudoers file so that they will be able to run Nexus as a service.

# Change /etc/sudoers 
sudo chmod 755 /etc/sudoers

# Edit sudoers file
sudo vi /etc/sudoers

# Add this line under "User privilege specification"
nexus ALL=(ALL) NOPASSWD:ALL
Enter fullscreen mode Exit fullscreen mode

Step 5: Configure Permissions

# Change ownership of Nexus directories
sudo chown -R nexus:nexus /opt/nexus
sudo chown -R nexus:nexus /opt/sonatype-work

# Set the user to run Nexus
sudo vi /opt/nexus/bin/nexus.rc
# Uncomment and set:
run_as_user="nexus"
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a Systemd Service

# Create a systemd unit file
sudo vi /etc/systemd/system/nexus.service
Enter fullscreen mode Exit fullscreen mode

Insert the following:

[Unit]
Description=Nexus Service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Restart=on-abort

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Step 7: Start Nexus Service

# Reload systemd and start Nexus
sudo systemctl daemon-reload
sudo systemctl start nexus

# Enable Nexus to start on boot
sudo systemctl enable nexus

# Check Nexus service status
sudo systemctl status nexus
Enter fullscreen mode Exit fullscreen mode

Expected output:

$ sudo systemctl status nexus
● nexus.service - nexus service
     Loaded: loaded (/etc/systemd/system/nexus.service; enabled; preset: enabled)
     Active: active (running) 
Enter fullscreen mode Exit fullscreen mode

🐳 Method 2: Using Docker

Installing Nexus with Docker is significantly easier and less complicated.

Step 1: Install Docker

If Docker is not installed, follow the official Docker installation guide.

Step 2: Run Nexus Container

# Pull the Nexus Docker image
sudo docker pull sonatype/nexus3

# Run the Nexus container
sudo docker run -d -p 8081:8081 --name nexus sonatype/nexus3

# Verify the container is running
sudo docker ps
Enter fullscreen mode Exit fullscreen mode

Expected output:

azureuser@Nexus:~$ sudo docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS         PORTS                                       NAMES
1a7bddff88b5   sonatype/nexus3   "/opt/sonatype/nexus…"   2 minutes ago   Up 2 minutes   0.0.0.0:8081->8081/tcp, :::8081->8081/tcp   nexus
Enter fullscreen mode Exit fullscreen mode

🌐 Accessing Nexus

Step 1: Open a web browser and navigate to: http://<your-server-ip>:8081

Step 2: Sign in with the default credentials:

  • Username: admin
  • Password: Found in:
    • /opt/sonatype-work/nexus3/admin.password (systemd)
    • /nexus-data/admin.password (Docker)

Retrieve the password:

# For systemd setup
cat /opt/sonatype-work/nexus3/admin.password

# For Docker setup
sudo docker exec -i nexus /bin/bash -c "cat /nexus-data/admin.password"
Enter fullscreen mode Exit fullscreen mode

Step 3: First sign in

You will be promoted by setup window wizard that will help you to complete required setup tasks, click next.

And change the password:

Password Change

Then configure anonymous access. You can enable or disable anonymous access. Choose what matches you needs.

Anonymous access choose


πŸ“‚ Creating a Nexus Repository

Signed in as admin, click the gear icon on the upper toolbar. Then, on the left, click Repositories, and you should see the Create Repository button.
Create repository

Choose the type of repository you want. You can read more about them here.

For this tutorial, I will go with the raw repository, where I will upload .txt files. Then, set up the repository by naming it and selecting the options you need. Finally, click Create Repository.

Repository settings

Now you should be able to see your repository on the dashboard. Click the cube icon next to the gear icon, then browse and look for your new repository. Click on it to see its contents.

Reposiotry Contents

As you can see, you can manually upload files to the repository, but it's inefficient. That's why we will use Jenkins for this πŸ”½πŸ”½πŸ”½


πŸ”— Connecting Nexus to Jenkins

I have a simple repository connected with Jenkins. I will upload the file requirements.txt to Nexus.

Step 1: Creating a Jenkins User in Nexus

You need to identify the user that is trying to push artifacts to the repository. Therefore, we need to add a Jenkins user in the Nexus repository. These credentials will be used in Jenkins.
nexus create user

Step 2: Installing the Nexus Plugin in Jenkins

The first thing you need to do is install the Nexus Plugin. Go to Manage Jenkins > Plugins. Type Nexus, and you will see theNexus Artifact Uploader. Check it and click Install.
nexus plugin

Step 3: Adding Nexus Credentials to Jenkins

In Jenkins, we need to add credentials.
πŸ”‘ Key Point: Use the same username and password in Jenkins as created in Nexus for seamless integration
Jenkins add Credentials

Step 4: Adding a Jenkinsfile

Finally, in your code repository, add a Jenkinsfile that includes a stage for uploading to Nexus, structured as follows:

You can specify the values as you wish and adjust the following parameters:

  • artifactId: The ID of the artifact you want to upload.
  • file: The path to the artifact you want to upload.
  • type: The type of artifact.
  • credentialsId: πŸ”‘ Key Point Use the credentials ID you named in Jenkins.
  • groupId: Specify as you wish; this will create the corresponding folder structure in the Nexus repository.
  • nexusUrl: The URL or IP address of your Nexus repository.
  • repository: The name of the repository.

βœ… Integration Check

Trigger Pipeline
Update a file (e.g., requirements.txt) and push changes to trigger the Jenkins pipeline.
Update requirements.txt

We can see that Jenkins was triggered and the upload to Nexus was successful.

Jenkins success build

Verify Upload
Navigate to your Nexus repository and confirm the artifact is uploaded.
Now we see that requirements.txt has been added to the repository with the correct path.

Nexus success upload

πŸ™Œ This concludes the tutorial. Thank you! πŸ™Œ

Feel free to leave any questions you have in the comments below! πŸ’¬ I'm here to help! 😊

πŸ“š Official Docs

✨ Nexus Documentation
✨ Jenkins Documentation

Top comments (0)