Nexus repository with Mule 4

  • September 14, 2020

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

Once you have downloaded the Windows version of Nexus OSS as a zip file:

  • 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

Install and Run Nexus OSS

  • 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 four 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 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 four repositories.

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

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>

Configure your Mule 4 project

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>

Configure your Mule 4 project

Nexus, Maven and Mule 4 in action

Run below command from command project within Mule 4 project directory.

  • mvn deploy

mvn deploy

mvn deploy

  • You can see the jar in maven-snapshots repository.

maven-snapshots repository

Note: As pom.xml version contains SNAPSHOT version, that’s the reason it got uploaded in maven-snapshots repository. Else, it will be uploaded in maven-releases repository.

— By Mohammad Mazhar Ansari