This article demonstrates how to integrate Maven with Nexus repositories and deploy snapshot and release artifacts automatically during the build process. It also helps to understand version uptick using Maven.
In this article we’ll cover:
- Creation of a hosted repository for snapshots and releases.
- Creation of a proxy repository pointing to Maven Central.
- Creation of a group repository.
- Integration of Maven with Nexus repositories.
- Configurations in the project pom.xml file.
- Configuration of the Jenkins release build.
Proxy Repository
A proxy repository refers to a remote repository. The initial request for a component is forwarded to the remote repository. The component is then retrieved and stored locally in the repository manager, which acts as a cache. Further requests for the same component are fulfilled from the local storage.
Hosted Repository
A hosted repository is a repository that stores components in the repository manager as the authoritative location for these components.
For release, follow the same configuration done for snapshots except Name, Version policy and Deployment policy.
Group Repository
The combination of multiple repositories of the same format into a single item is a repository group.
This feature of the Nexus Repository Manager lets developers rely on a single URL for their configuration needs.
Create a new Maven (group) repository and configure it like:
Integration of Maven with Nexus Repositories
Nexus repositories are integrated with Maven using the Mavens settings.xml file. This file is usually present in ($HOME/.m2, in case of Jenkins /var/lib/Jenkins/.m2) for user-level configuration. Configurations in this file are applied for all the Maven builds initiated by the user.
There are two ways Maven can utilize the Nexus/other artifactory in general. One is to download the dependencies and use the artifactory as mirror during the build, and the other is to upload the built artifacts into the repositories.
Each repository in the Nexus and the access credentials are configured in this settings file, so that the repository can be referred in pom.xml and utilized to build the project artifacts and upload them.
<?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>*******</password>
</server>
<servers>
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>*******</password>
</server>
<servers>
<mirrors>
<mirror>
<id>central</id>
<name>central</name>
<url>http://hostname:8081/repository/maven-group/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>
Configurations in the Project pom.xml File
The following configuration is for downloading the dependencies from maven group repository:
<project …>
<repositories>
<repository>
<id>maven-group</id>
<url>http://hostname:8081/repository/maven-group/</url>
</repository>
</repositories>
</project>
And this is the configuration for publishing snapshot and release artifacts to Nexus:
<project …>
<distributionManagement>
<snapshot>
<id>nexus-snapshots</id>
<url>http://hostname:8081/repository/maven-snapshots/</url>
</snapshot>
</distributionManagement>
</project>
<project …>
<distributionManagement>
<releases>
<id>nexus-releases</id>
<url>http://hostname:8081/repository/maven-releases/</url>
</releases>
</distributionManagement>
</project>
Add the tag to the SCM for the Maven build:
<scm>
<connection>scm:git:http://gitlab.com/user/project.git</connection>
<url>http://gitlab.com/user/project</url>
<developerConnection>scm:git:http://gitlab.com/user/project.git</developerConnection>
</scm>
Run – clean deploy in Maven goals for Jenkins snapshot build.
Configuration of Jenkins Release Build
In the Jenkins build configuration, enable the a Maven release build with required release goal. Also, make sure to select Preselect ‘Specify SCM login/password,’ which is required when performing Maven release with authentication.
After the snapshot build completion, the below inputs are needed to perform the Maven release. Here, username and password refer to the GitLab admin credentials.
Conclusion
By following the above steps, Maven and the Nexus artifactory can be integrated at the user level and the Maven execution for that user inherits this setting. A similar setting can be done at the system level with the Maven global tool setting.
These steps help project teams with one-time configuration of snapshot and release artifacts upload to Nexus, and a version uptick of pom.xml in GitLab. Similar settings can be performed for other artifactories such as JFrog.