Author: Mohammad Mazhar Ansari
In this blog, we will try to learn how to use our own Nexus Repository with Mule 4.
What is Nexus OSS?
- Nexus Repository OSS is an open source repository that supports many artifact formats, including Docker, Java™, and npm.
- With the Nexus tool integration, pipelines in your toolchain can publish and retrieve versioned apps and their dependencies by using central repositories that are accessible from other environments.
How to download Nexus OSS?
- You can download Nexus OSS by registering here.
- However, I prefer the docker image which I downloaded from here.
Install and Run Nexus OSS
If you have downloaded the Windows version of Nexus OSS as a zip file, then
- Unzip Nexus OSS zip file in folder called <Nexus-Home>
- Go to <Nexus-Home>
- Open command prompt
- Run command “nexus.exe /run”
If you have docker image on a unix system like I did, then
- docker pull sonatype/nexus3
- mkdir /opt/docker/nexus-data && chown -R 200 /opt/docker/nexus-data
- docker run -d -p 8081:8081 –name nexus -v /opt/docker/nexus-data:/nexus-data sonatype/nexus3
- /opt/doker/nexus-data is custom directory i created for Mount a host directory as the volume
- Open http://<your-h0st>:8081
- Click on Sign in
- Default username is admin and password is admin123
Configuring Nexus as a Maven repo:
To use Nexus as maven repo, we need to create 4 repositories.
- create a private (hosted) repository for our snapshots (maven-snapshots)
- create a private (hosted) repository for our releases (maven-releases)
- create a proxy repository pointing to Maven Central (maven-central)
- create a group repository to provide all of these repos under a single URL (maven-public)
I suggest you to create a new blob store for each new repo you want to create. In that way, the data for every repo will be in a different folder in /nexus-data (inside the Docker container). But this step is not mandatory for it to work.
The Docker image that I had created already has all 4 repositories.
Configuring Nexus repos in Maven:
Update <User-Home>/.m2/settings.xml file
- Add below lines under <servers>
<server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>nexus-snapshots</id> <username>admin</username> <password>admin123</password> </server>
- Add below lines under <mirrors>
<mirror> <id>central</id> <name>central</name> <url>http://your-host:8081/repository/maven-group/</url> <mirrorOf>*</mirrorOf> </mirror>
Configure your Mule 4 project:
If you want only to download dependencies from Nexus, add below lines under project/repositories in project’s pom.xml.
<repository> <id>maven-group</id> <url>http://your-host:8081/repository/maven-group/</url> </repository>
And if you also want to publish your project, add below lines under project/distributionManagement in project’s pom.xml.
<snapshotRepository> <id>nexus-snapshots</id> <url>http://your-host:8081/repository/maven-snapshots/</url> </snapshotRepository> <repository> <id>nexus-releases</id> <url>http://your-host:8081/repository/maven-releases/</url> </repository>
Nexus, Maven and Mule 4 in Action:
Run below command from command project within Mule 4 project directory.
- mvn deploy
- You can see the jar in maven-snapshots repository.
Note: As pom.xml version contains SNAPSHOT version, that is the reason why it got uploaded in maven-snapshots repository. Else, it will be uploaded in maven-releases repository.