How to Set Up and Use a Maven Private Repository with Nexus and Docker
This tutorial explains what a Maven private repository is, walks through setting up Nexus as a private repo using Docker, and shows how to configure Maven and deploy both project artifacts and third‑party JARs, enabling faster, secure builds for Java enterprise projects.
Introduction
In Java EE development Maven is used to manage third‑party libraries and internal services; a Maven private repository (private repo) is therefore essential. This article covers three aspects: what a Maven private repository is, how to set it up, and how to use it.
What is a Maven Private Repository
A Maven repository stores all project dependencies, plugins and uploaded artifacts. There are local and remote repositories. A private repository is a special remote repository deployed inside a LAN, acting as a proxy for external repositories and allowing internal projects to upload and retrieve artifacts.
It reduces external bandwidth, speeds up builds, secures private libraries, improves build stability and enables lightweight internal service references.
Setting Up a Maven Private Repository
The guide uses Sonatype Nexus OSS, installed via Docker for quick and lightweight deployment.
Download Nexus Docker Image
Run: docker pull sonatype/nexus3 Verify with:
docker imagesStart Nexus Container
Execute:
docker run -d -p 8081:8081 -v /Users/One/Desktop/Nexus/nexus-data:/nexus-data --restart=always sonatype/nexus3After a short wait, access http://localhost:8081/ to confirm the service is running.
If the container starts slowly, just wait for it to become ready.
Configuring Nexus
Log in with the default admin account (password stored in /nexus-data/admin.password) and change the password.
Adjust the proxy for the central repository to use Alibaba Cloud mirror: https://maven.aliyun.com/repository/central.
Create a hosted repository named private-release (Release policy, allow redeploy) and a snapshot repository private-snapshot (Snapshot policy).
Add a custom role developer with repository usage permissions, then create local users and assign the role.
Using the Maven Private Repository
Add server credentials and repository definitions to settings.xml (server id, username, password, and repository URLs).
In a Maven project, add a <distributionManagement> section pointing to the private repo and run: mvn clean deploy Successful deployment shows BUILD SUCCESS and the artifact appears in the Nexus repository.
For SNAPSHOT versions Maven uploads to the snapshot repository; for release versions it uploads to the release repository.
Deploying Third‑Party JARs
Third‑party JARs can be uploaded via the Nexus UI or using the Maven deploy plugin from the command line:
mvn deploy:deploy-file -DgroupId=... -DartifactId=... -Dversion=... -Dpackaging=jar -Dfile=/path/to/jar -Durl=http://localhost:8081/repository/private-release/ -DrepositoryId=private-releaseEnsure the user has upload permissions; otherwise an “Access denied” error will occur.
Conclusion
Setting up and maintaining a Maven private repository is a crucial step in enterprise Java projects, providing faster, more reliable builds and a secure way to share internal libraries.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
