This tutorial shows you how to install Nexus on your computer with docker.

Download

Distributions for Nexus Repository Manager 3 are available for the 64-bit versions for Apple OSX, Microsoft Windows and Unix/Linux in this link.

In this article I use Docker Hub Image of Nexus on macOS :

Nexus 3 will go up on port 8881. Default credentials are admin/admin123.

Docker Pull Command :

$ docker pull sonatype/nexus3

Capture-d-e-cran-2018-12-24-a--12.33.37

$ docker run -d -p 8881:8081 --name nexus sonatype/nexus3
# run logs command and wait until the Sonatype Nexus OSS Started
$ docker logs -f nexus

Capture-d-e-cran-2018-12-24-a--12.51.51

# Default credentials are: admin / admin123
# To test run: 
$ curl -u admin:admin123 http://localhost:8881/service/metrics/ping

Capture-d-e-cran-2018-12-24-a--12.54.20

Configuring Nexus as a Maven repo

This Image Docker come with  :

  • Private (hosted) repository for our snapshots
  • Private (hosted) repository for our releases
  • Proxy repository pointing to Maven Central

And I Create Group repository

  • Group repository to provide all of these repos under a single URL

Snapshots repo

This repository for Maven artifacts that you deploy with -SNAPSHOT in the end of the version tag of your pom.xml:

<version>1.0.0-SNAPSHOT</version>

Releases repo

A repository for Maven artifact that you deploy without -SNAPSHOT in the end of the version tag of your pom.xml:

<version>1.0.0</version>

Proxy to Maven Central repo

A repository that proxies everything you download from Maven Central. Next time you download the same dependency, it will be cached in your Nexus.

group repo

This will group all the above repos and provide you a single URL to configure your clients to download from/deploy to.

Create a new maven (group) repository and configure it like:

How to interact with Nexus Repository Manager programmatically :

You can get the swagger UI in this URL :  http://localhost:8881/swagger-ui/

For example to get list of all repositories :

$ curl -X GET "http://localhost:8881/service/rest/v1/repositories" -H "accept: application/json"

http://localhost:8881/service/rest/v1/repositories

Configuring your clients and projects to use your Nexus repos

Put this in your ~/.m2/settings.xml file. This will configure the credentials to publish to your hosted repos, and will tell your mvn to use your repo as a mirror of central:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">

  <servers>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors>
    <mirror>
      <id>central</id>
      <name>central</name>
      <url>http://localhost:8881/repository/maven-group/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>

</settings>

$ cd /Users/rcherara/.m2
$ vi settings.xml

Capture-d-e-cran-2018-12-24-a--13.32.24

And now configure your projects.

Note : As you see in my last screenshot, I have configured two others repos. Docker Hub and S3 repos,  I will explain with a new article soon how to configure them in new article shortly.

If you want only to download dependencies from Nexus, put this in the pom.xml:

<project ...>

  ...

  <repositories>
    <repository>
      <id>maven-group</id>
      <url>http://localhost:8881/repository/maven-group/</url>
    </repository>
  </repositories>
</project>

And if you want also to publish your project, add:

<project ...>

  ...

  <distributionManagement>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <url>http://localhost:8881/repository/maven-snapshots/</url>
    </snapshotRepository>
    <repository>
      <id>nexus-releases</id>
      <url>http://localhost:8881/repository/maven-releases/</url>
    </repository>
  </distributionManagement>
</project>

Now if you run in your projects:

$ mvn install
# or
$ mvn deploy

Capture-d-e-cran-2018-12-24-a--13.44.34
Capture-d-e-cran-2018-12-24-a--13.46.24

your mvn will point to your Nexus instance. and you can see the deployment of  your artifact  on Nexus :