News and Notes from the Makers of Nexus | Sonatype Blog

Using Sonatype Nexus Repository 3 – Part 1: Maven Artifacts

Written by Rafael Eyng | February 21, 2017

This article is the first in a three part series by one of our community advocates, Rafael Eyng.

NOTE: We are constantly improving Sonatype Nexus Repository. This blog may no longer be accurate. You can find the latest instructions here.

Installation

Install it with Docker:

docker run -d -p 8081:8081 -p 8082:8082 -p 8083:8083 
--name my-nexus sonatype/nexus3:3.0.0

We are mapping all of those ports (8081-8083) because of the next posts in the series. For this post, we'll actually only need port 8081.

Sonatype Nexus Repository 3 will go up on port 8081. Default credentials are admin/admin123.

You might want to create a volume to map the Sonatype Nexus Repository data folder to your host, adding the option -v /opt/my-nexus-data:/nexus-data.

Configuring Sonatype Nexus Repository as a Maven repository

What we will do:

  • Create a private (hosted) repository for our snapshots.
  • Create a private (hosted) repository for our releases.
  • Create a proxy repository pointing to Maven Central.
  • Create a group repository to provide all of these repositories under a single URL.

I suggest you to create a new blob store for each new repository you want to create. That way, the data for every repository will be in a different folder in /nexus-data (inside the Docker container). But this is not mandatory for it to work.

Snapshots repository

A 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>

Create a new Maven (hosted) repository and configure it like so:

Releases repository

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>

Create a new Maven (hosted) repository and configure it like so:


Proxy to Maven Central repository

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

Create a new Maven (proxy) repository and configure it like so:

 

Group repository

This will group all the above repositories 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 so:

You can create as many repositories as you need (like proxies to other public repositories) and group them all in the group repository.

Configuring your clients and projects to use your Sonatype Nexus Repository

Put this in your ~/.m2/settings.xml file. This will configure the credentials to publish to your hosted repositories, and will tell your mvn to use your repository 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://your-host:8081/repository/maven-group/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>

</settings>

And now configure your projects.

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

<project ...>

  ...

  <repositories>
    <repository>
      <id>maven-group</id>
      <url>http://your-host:8081/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://your-host:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
    <repository>
      <id>nexus-releases</id>
      <url>http://your-host:8081/repository/maven-releases/</url>
    </repository>
  </distributionManagement>
</project>

Now if you run in your projects:

mvn install
# or
mvn deploy

your mvn will point to your Sonatype Nexus Repository instance.