On medium/large projects, it is common practice to control the external Maven repositories through an internal repository manager. Maven's central repository is very convenient for the users of Maven, but it is recommended to maintain your own repositories to ensure stability within your organization. Just as Software Configuration Management (SCM) tools are designed to manage source artifacts, repository managers have been designed to manage external dependencies and artifacts generated by your own build.
Consider an organization that has 100 developers split into different groups, each group working on a different part of the system without an easy way to share internal dependencies, and every group creating an ad hoc filesystem-based repository or building the system in its entirety so that dependencies are installed in every developer's local repository. Indeed, if your application is being continuously built and deployed using a tool such as Hudson (which we will discuss later), a developer can get a specific module from a large project build and not have to constantly compile the entire source at any given time.
Internal repository managers offer some advantages, such as:
Nexus Open Source or Professional is one of the most common repository managers (an alternative is Artifactory); Nexus has a very flexible infrastructure and allows us to configure multiple environments for different teams.
There are two distributions of Nexus: Nexus Open Source and Nexus Professional. For our purpose, we will use Nexus Open Source (referred to as Nexus for short), which is distributed under the GNU Affero General Public License Version 3. Nexus is a Java web application and can be downloaded from http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz.
Nexus can be run with a Jetty instance that runs on port 8081 by default, but should be installed on a different servlet container.
Finally, the location of the work directory can be customized by altering the nexus-work
property in /usr/local/conf/nexus.properties
or <NEXUS HOME>/conf/nexus.properties
.
To test the correct installation, we can open the browser to http://<nexus_host>:8081/
.
The default username and password are admin
and admin123
, respectively.
The Nexus server can be easily configured through the administrative console. On the left-hand side menu, navigate to Administration | Server to view the administrative settings console. Since Nexus has to access the remote repository, it is strongly encouraged to configure the proxy. On the administrative settings console (called nexus), enable the Default HTTP Proxy settings and configure them using your organization proxy (see the following screenshot):
Since Nexus is configured to proxy the most common public repositories such as Central Repository or Apache Repository, to test the Internet connection of Nexus, we can download maven-ejb-plugin
directly from our local Nexus instance using this link: http://<nexus_host>:8081/nexus/content/repositories/central/maven/maven-ejb-plugin/1.7.3/maven-ejb-plugin-1.7.3.pom
.
The maven-ejb-plugin
artifact is downloaded on the local Nexus working directory and is cached for further applications. We can browse the local cached repository (see the following screenshot) on the left-hand side menu; the Repositories item opens a list of repositories and we can use the Browse Index menu to explore the index of Nexus.
When Nexus works properly, we can configure our pom.xml
file to use it. We have to configure both the official and custom repositories to allow the download of artifacts.
Maven is configured by default to use the official central repository, http://repo1.maven.org
.
To change the declared default repository to point to the installed Nexus repository, we have to configure the <repository>
and <pluginRepository>
tags in the pom.xml
file:
<repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Proxied Maven Repository</name> <url> http://<nexus_host>:8081/nexus/content/ repositories/central/ </url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Proxied Maven Plugin Repository</name> <url>http://<nexus_host>:8081/nexus/content/repositories/central/</url> </pluginRepository> </pluginRepositories>
To add other proxy repositories, access the online Nexus console and, from the repositories view, navigate to Add… | Hosted Repository (see the following screenshot); you can then configure the repository. In this case, the repository has been declared as a not snapshot repository through the following tag:
[…]
<snapshots>
<enabled>false</enabled>
</snapshots>
[…]
By default, the snapshots
and releases
tags are true
, which means that the repository is enabled for both the snapshot and release artifacts (refer to Chapter 2, Core Maven Concepts).
The User Managed Repository is the most important functionality provided by Nexus. Configuring a User Managed Repository is quite simple, requiring only a couple of steps. From the online Nexus console, navigate to Add… | Proxy Repository (see the following screenshot):
Nexus requires the name and ID of the repository (see the following screenshot). The ID is the identifier of the repository; it will be part of the URL and cannot contain spaces.
By default, Nexus is configured with three user repositories:
For our purposes, these repositories are adequate.
Nexus provides other important functionalities such as fine-grained access-level security. By default, Nexus allows an anonymous user to work with all the repositories. If your organization needs some specific policies for each group, the security view allows you to define an external LDAP server or add custom users to Nexus. Finally, through the privileges view, it is easy to grant or revoke some specific levels.