Configure Nexus Repository for Maven Artifact Storage

🎯 Project Objective

Set up Sonatype Nexus Repository Manager on Amazon Linux 2 to store and manage Maven artifacts created during your CI/CD process using Jenkins.


🧰 Tools Used

  • Amazon EC2 (Amazon Linux 2)

  • Sonatype Nexus Repository Manager OSS

  • Jenkins + Maven (from previous days)


🚀 Step-by-Step Procedure

1️⃣ Launch EC2 Instance (If not already running)

  • Type: t2.medium or higher

  • Ports to open in Security Group:

    • 8081 (Nexus UI)

    • 22 (SSH)


2️⃣ Install Java 8 (Required for Nexus)

sudo yum install java-1.8.0-openjdk -y

3️⃣ Download & Setup Nexus

cd /opt
sudo wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
sudo tar -xvzf latest-unix.tar.gz
sudo mv nexus-3* nexus
sudo useradd nexus
sudo chown -R nexus:nexus /opt/nexus

Edit /opt/nexus/bin/nexus.rc:

run_as_user="nexus"

4️⃣ Create a Systemd Service

sudo nano /etc/systemd/system/nexus.service

Paste:

[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
sudo systemctl daemon-reexec
sudo systemctl enable nexus
sudo systemctl start nexus

5️⃣ Access Nexus UI

Open:
http://<your-ec2-public-ip>:8081

Login:

  • Username: admin

  • Password: (find it in /opt/sonatype-work/nexus3/admin.password)


6️⃣ Create a Maven Hosted Repository

  • Go to Settings → Repositories → Create Repository

  • Choose maven2 (hosted)

  • Name: maven-releases

  • Version policy: Release

  • Deployment: Allow redeploy

  • Save


7️⃣ Configure Maven to Use Nexus

Edit or create ~/.m2/settings.xml:

<servers>
  <server>
    <id>nexus</id>
    <username>admin</username>
    <password>yourpassword</password>
  </server>
</servers>

<profiles>
  <profile>
    <id>nexus</id>
    <repositories>
      <repository>
        <id>central</id>
        <url>https://repo1.maven.org/maven2</url>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>central</id>
        <url>https://repo1.maven.org/maven2</url>
      </pluginRepository>
    </pluginRepositories>
  </profile>
</profiles>

<activeProfiles>
  <activeProfile>nexus</activeProfile>
</activeProfiles>

In your Maven pom.xml, set:

<distributionManagement>
  <repository>
    <id>nexus</id>
    <url>http://<your-ec2-ip>:8081/repository/maven-releases/</url>
  </repository>
</distributionManagement>

Then publish:

mvn deploy

Expected Output

  • Nexus Repository accessible at http://<public-ip>:8081

  • Maven artifacts can be deployed and stored in Nexus

  • Ready to integrate into Jenkins CI/CD pipeline

0
Subscribe to my newsletter

Read articles from Suneel Kumar Kola directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Suneel Kumar Kola
Suneel Kumar Kola