Step-by-Step Guide to Building a Maven Private Repository with Nexus
This tutorial explains why a private Maven repository is useful for Java teams, walks through downloading and installing Nexus on Linux, configuring repositories, managing firewall rules, setting up anonymous access, and publishing and retrieving artifacts using Maven settings and commands.
Maven Overview
Maven, maintained by the Apache Software Foundation, provides build and dependency‑management for Java projects. It resolves declared dependencies from a remote repository (by default Maven Central) and avoids manual JAR handling and version conflicts.
Reason for a Private Maven Repository
In a corporate setting, teams share the same dependencies. Hosting artifacts on an internal Nexus server reduces project initialization time, eliminates external network latency, and prevents accidental code leakage.
Installing Nexus
Download the latest Nexus tarball from https://download.sonatype.com/nexus/3/latest-unix.tar.gz, upload it to a Linux host (e.g., CentOS 7), extract it to /opt/nexus, and ensure a JDK is installed.
Start, check status, and stop Nexus with:
# start
/opt/nexus/nexus-3.61.0-02/bin/nexus start
# status
/opt/nexus/nexus-3.61.0-02/bin/nexus status
# stop
/opt/nexus/nexus-3.61.0-02/bin/nexus stopWhen the log shows nexus is running , Nexus listens on port 8081 . If the log shows nexus is stopped , edit /opt/nexus/nexus-3.61.0-02/etc/nexus-default.properties to change the port (e.g., to 8090 ).
Opening the Firewall
# ensure firewalld is running
sudo systemctl start firewalld
# enable at boot
sudo systemctl enable firewalld
# open TCP port 8090 permanently
sudo firewall-cmd --zone=public --add-port=8090/tcp --permanent
# reload rules
sudo firewall-cmd --reload
# verify open ports
sudo firewall-cmd --zone=public --list-portsInitial Nexus Login
Open http://[server_ip]:8090/, log in with the default admin user, and retrieve the initial password from /opt/nexus/sonatype-work/nexus3/admin.password.
Repository Types
proxy : proxies a remote repository such as Maven Central or an Alibaba Cloud mirror.
group : aggregates multiple repositories for unified access.
hosted : stores artifacts uploaded by internal teams.
Default Repositories Created by Nexus
maven-central : proxy of Maven Central.
maven-public : group repository that includes the three hosted repositories.
maven-releases : hosted repository for release versions.
maven-snapshots : hosted repository for snapshot versions.
The maven-public group combines the other three repositories; additional custom repositories can be added to it.
Creating Custom Repositories
Use the Nexus UI “Create Repository” button and select one of the three types:
Hosted : provide a repository name and create it.
Proxy : provide a name and the URL of the remote repository (e.g., the Alibaba Cloud mirror).
Group : add existing repositories to form a combined view.
Configuring Maven to Use Nexus
Add a <mirror> entry to settings.xml that points to the Nexus maven-public group:
<mirror>
<id>maven-public</id>
<mirrorOf>central</mirrorOf>
<name>Maven public</name>
<url>http://192.168.11.164:8090/repository/maven-public/</url>
</mirror>If anonymous access is disabled, add a matching <server> entry with credentials:
<server>
<id>maven-public</id>
<username>admin</username>
<password>admin</password>
</server>Verify the configuration by creating a Maven project that depends on fastjson2 and confirming the JAR is downloaded through Nexus.
Deploying Artifacts to Nexus
Configure settings.xml with <server> entries for the releases and snapshots repositories (using the same admin credentials):
<server>
<id>maven-releases</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>Add a <distributionManagement> section to the project's pom.xml:
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Releases Repository</name>
<url>http://192.168.11.164:8090/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Snapshot Repository</name>
<url>http://192.168.11.164:8090/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>Running mvn deploy uploads the built artifact. Projects with a version ending in -SNAPSHOT are stored in maven-snapshots; changing the version to a release number (e.g., 1.0.0 ) directs the upload to maven-releases.
Common Maven Commands
package : compiles, runs tests, and packages the project.
install : performs package and copies the artifact to the local Maven repository.
deploy : performs package and uploads the artifact to a remote Maven repository such as the Nexus private server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
