Nexus Repository Manager - Proxying Maven and npm Quick Start

Repository Manager | Reading time: 5 minutes

If you’re new to repository management with Nexus Repository Manager 3, use this guide to get familiar with configuring the application as a dedicated proxy server for Maven and npm builds. To reach that goal, follow each section to:

  • Install Nexus Repository Manager 3
  • Run the repository manager locally
  • Proxy a basic Maven and npm build

When you complete the steps, the components will be cached locally to your repository manager. After meeting the requirements to run the repository manager, it should take approximately 15 minutes to proxy Maven and npm with the code snippets below.

Nexus Repository Manager - Proxying Maven and npm Quick Start Video

 

Requirements

Before you can set up the proxy server for Maven and npm, you’ll need to install and configure the following external tools for the repository manager:

Java 8 Development Kit (JDK) - Nexus Repository Manager is a Java server application. As explained in System Requirements, we strongly recommend using Java 8 to ensure effective runtime of Nexus Repository Manager 3.

Apache Maven - When downloaded, Nexus Repository Manager 3 includes access to open source components from the Central Repository by default. These components are defined by both a settings.xml file and POMs, which maintain information on projects and dependencies.

npm - A popular format supported by the repository manager. Unlike Maven, Nexus Repository Manager 3 doesn’t currently ship with a default npm repository configuration. These components are configured with an .npmrc file.

Part 1 - Installing and Starting Nexus Repository Manager 3

  1. Create an installation directory in your desired location.
  2. Download the most recent repository manager for your operating system.
  3. If the file is downloaded to a location outside the installation directory, move it there.
  4. Unpack the .tar.gz or .zip file in its new location. Both an application (i.e. nexus-<version>) and data directory (i.e. ../sonatype-work/nexus3) are created after extraction.
  5. Go to the application directory which contains the repository manager file you need to start up.
  6. In the application directory, run the startup script launching the repository manager:
    • Linux or Mac: ./bin/nexus run
    • Windows: bin/nexus.exe /run
  7. Wait until the log shows the message “Started Sonatype Nexus.”
  8. Open your browser and type http://localhost:8081/ in your URL field.
  9. From the user interface click Sign In, which generates a modal to enter your credentials.
  10. Log in with the default credentials admin / admin123, the respective username / password.
  11. Change the default password!

Part 2 - Proxying Maven and npm Components

When you proxy components the repository manager acts as a local intermediary server for any download requests going to remote repositories / registries. After logging in, these next steps will show you how to configure then test your configuration with local builds for a Maven and npm project.

Maven

NOTE: If you have an existing Maven configuration file (settings.xml) that you want to retain, back it up before doing any modifications.

  1. In your file system, open your settings.xml and change the contents of the snippet below. You can find this file in .m2, e.g ~/.m2/settings.xml.
<settings>
  <mirrors>
    <mirror>
    <!--This sends everything else to /public -->
    <id>nexus</id>
    <mirrorOf>*</mirrorOf>
    <url>http://localhost:8081/repository/maven-proxy/</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
    <id>nexus</id>
    <!--Enable snapshots for the built in central repo to direct -->
    <!--all requests to nexus via the mirror -->
    <repositories>
        <repository>
        <id>central</id>
        <url>http://central</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
        <id>central</id>
        <url>http://central</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>
  1. Go to the repository manager user interface.
  2. Click Administration in the left navigational menu, then click Repositories.
  3. Click Create repository and choose the maven2 (proxy) recipe from the list.
  4. Add the following text in the required fields:
    • Name: maven-proxy
    • Remote storage URL: https://repo1.maven.org/maven2
  5. Click Create repository to complete the form.
  6. From the command-line interface, create the POM file (pom.xml) with the values below:
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>nexus-proxy</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
  </dependencies>
</project>
  1. Run the Maven build with the command mvn package.

NOTE: If you to want to view the details of your Maven test build, skip to Part 3. However, if you want to test an npm build, continue to the next section in this part.

npm

NOTE: If you have an existing npm configuration file (.npmrc) that you want to retain, back it up before doing any modifications.

  1. Click Administration in the left navigational menu, then click Repositories.
  2. Click Create repository and choose npm (proxy) from the list.
  3. Add the following text in the required fields:

    • Name: npm-proxy
    • Remote Store URL: https://registry.npmjs.org/
  4. From the command-line interface run npm config set registry http://localhost:8081/repository/npm-proxy.

  5. From the command-line interface, create a package.json with the values below:

{
  "name": "sample_project1",
  "version": "0.0.1",
  "description": "Test Project 1",
  "dependencies" : {
    "commonjs" : "0.0.1"
  }
}
  1. Run the npm build with the command npm install.

Part 3 - Viewing Proxied Components

After your Maven and npm projects are successfully built, follow these steps to view the cached components:

  1. Click Browse from the main toolbar.
  2. Click Components.
  3. Of your components, choose maven-proxy or npm-proxy. You’ll see the test component you proxied for the respective format during the previous build steps.
  4. Click on a component name to review its details.
  5. The Components screen is a sub-section to the Browse interface. In order to view other components, click Components directly from the current screen and select another repository name from step 3 in this part.