Exploring Nexus Repository Manager: A Journey Through Hosted, Proxy, and Group Repositories
Tags: devops, nexus, maven, java
Published: 2024-02-19 05:23:23

Hey Dev folks,

Ready to dive into the magic of Nexus Repository Manager? Let's chat about hosted, proxy, and group repositories – the pillars that elevate Nexus to a pinnacle in Maven artifact orchestration.

## Introduction

Nexus Repository Manager is a tool that helps developers store and manage their artifacts, such as libraries, packages, and images, in different formats. There are mainly three types of repositories in Nexus: __hosted__, __proxy__, and __group__.

## Hosted Repository

_A hosted repository is a repository that stores the artifacts that you create or publish in Nexus_. It is the authoritative source for these artifacts, meaning that they are not available anywhere else.

You can use a hosted repository to share your own artifacts with other developers, or to store third-party artifacts that are not accessible from a public repository.

For example, you can create a hosted repository for your Maven projects, and publish your jar files there. You can also use a hosted repository to store proprietary libraries, such as an Oracle JDBC driver, that you need for your projects.

To create a hosted repository, you need to specify the repository format, such as Maven, and the version policy, such as release, snapshot or mixed. You can also configure the deployment policy, which determines who can publish artifacts to the repository, and the storage settings, such as the blob store and the cleanup policy. Once you create a hosted repository, you can access it from your build tools or browsers using the repository URL.

Here is an example of how to create a hosted repository for Maven projects in Nexus:

- Login to the nexus with appropriate credentials.
- Click on Repositories on the sidebar.
- Then select `Create repository` button and choose the type of recipe. For this case, it would be `maven2(hosted)`.
- After selecting the recipe, configure the repository.

A sample data is shown below:

| Field | Value |
| --- | --- |
| Repository Name | my-maven-repo |
| Version Policy | Release |
| Deployment Policy | Allow Redeploy |
| Blob Store | default |

- Click on Create repository.

Now you have a custom repository, which is hosted on your local nexus installation. You can also add this repository to your pom.xml file, so that you can use it as a dependency source for your projects. For example:

```xml
<project>
  ...
  <repositories>
    <repository>
      <id>my-maven-repo</id>
      <url>http://localhost:8081/repository/my-maven-repo/</url>
    </repository>
  </repositories>
  ...
</project>
```

You can publish your Maven artifacts to this repository using the following steps:

1. First add the maven plugin in `pom.xml`:
     ```xml
    <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>3.1.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
        </plugin>
    </plugins>
    </build>
    ```
2. Now add the location of nexus repo in `pom.xml`:

    ```xml
    <project>
      ...
      <distributionManagment>
        <snapshotRepository>
          <id>nexus-snapshots</id>
          <url>http://localhost:8081/repository/my-maven-repo/</url>
        </snapshotRepository>
      </distributionManagment>
      ...
    </project>
    ```
    You can use any `id` you want to reference it. And in `url`, add you repository URL where you want to upload the packages.

3. Next, add `settings.xml` file in `~/.m2` folder and add credentials. These will then be used at global level.

    ```xml
    <settings>
      <servers>
        <server>
          <id>nexus-snapshots</id>
          <username>test</username>
          <password>password</password>
        </server>
      </servers>
    </settings>
    ```
    Make sure that, the `id` is same as you have configured in the `pom.xml` file.

4. Now build the package and upload it to repo:

    ```bash
    # building the package
    mvn package

    # now upload the package
    mvn deploy
    ```

## Proxy Repository

_A proxy repository is a repository that links to a remote repository, such as Maven Central, and caches the artifacts that are requested from it_. This way, you can reduce the network bandwidth and latency of retrieving the artifacts from the remote repository every time you need them.

You can also use a proxy repository to access artifacts that are not available in your hosted repository, or to enforce security policies on the artifacts that you download.

To create a proxy repository, you need to specify the repository format, such as Maven, and the remote URL of the repository that you want to proxy. You can also configure the storage settings, such as the blob store and the cleanup policy. Once you create a proxy repository, you can access it from your build tools or browsers using the repository URL.

Here is an example of how to create a proxy repository for Maven Central in Nexus:

- Login to the nexus with appropriate credentials.
- Click on Repositories on the sidebar.
- Then select `Create repository` button and choose the type of recipe. For this case, it would be `maven2(proxy)`.
- After selecting the recipe, configure the repository.

| Field | Value |
| --- | --- |
| Repository Name | maven-central |
| Version Policy | Release |
| Remote Storage Location | https://repo1.maven.org/maven2/ |
| Blob Store | default |
| Cleanup Policy | none |

- Click on Create repository.

Now you have a proxy repository, which mirrors the Maven Central repository. You can use this repository as a dependency source for your projects, by adding it to your pom.xml file. For example:

```xml
<project>
  ...
  <repositories>
    <repository>
      <id>maven-central</id>
      <url>http://localhost:8081/repositories/maven-central/</url>
    </repository>
  </repositories>
  ...
</project>
```

## Group Repository

_A group repository is a repository that aggregates multiple repositories, such as hosted and proxy repositories, into a single repository_. This way, you can simplify the configuration of your build tools or browsers, by using only one repository URL instead of multiple ones.

You can also use a group repository to provide a consistent view of the available artifacts, regardless of their origin.

To create a group repository, you need to specify the repository format, such as Maven, and the members of the group, which are the repositories that you want to aggregate. You can also configure the storage settings, such as the blob store. Once you create a group repository, you can access it from your build tools or browsers using the repository URL.

Here is an example of how to create a group repository for Maven projects in Nexus:

- Login to the nexus with appropriate credentials.
- Click on Repositories on the sidebar.
- Then select `Create repository` button and choose the type of recipe. For this case, it would be `maven2(group)`.
- After selecting the recipe, configure the repository.

| Field | Value |
| --- | --- |
| Repository Name| maven-group |
| Members | my-maven-repo, maven-central |
| Blob Store | default |

- Click on Create repository.

Now you have a group repository, which combines your hosted and proxy repositories. You can use this repository as a dependency source for your projects, by adding it to your pom.xml file. For example:

```xml
<project>
  ...
  <repositories>
    <repository>
      <id>maven-group</id>
      <url>http://localhost:8081/repository/maven-group/</url>
    </repository>
  </repositories>
  ...
</project>
```

## Conclusion

In this blog, I have shown you how to use Nexus Repository Manager for Maven projects, how to create and configure different types of repositories in Nexus, and how to upload your package to repository. I hope you have learned something useful, and I encourage you to try out Nexus for your own projects. You can find more information and documentation about Nexus on the [official website](https://help.sonatype.com/en/maven-repositories.html).

Happy coding! 😊