Maven internal repository

1. Set Up a Private Maven Repository

First, you need to set up a Maven repository in the internal network environment. Common private Maven repository tools are:

  • Nexus Repository: Sonatype Nexus is the most popular private Maven repository management tool.
  • Artifactory: JFrog Artifactory is another popular build management tool that provides support for private repositories.
  • Apache Archiva: Apache Archiva is also a repository management tool that supports Maven.

The following are the simple steps to set up Nexus Repository:

1.1 Install Nexus Repository

  • Download Nexus: Visit the Nexus Repository download page and download the Nexus OSS version.

  • Extract and start: Extract the downloaded archive and start Nexus.

1
2
3

cd /opt/nexus/bin
./nexus start
  • Access the Nexus UI: Open your browser and visit the default Nexus address:
1
http://localhost:8081

The default username is admin and the password is admin123; you can change the password after logging in for the first time.

1.2 Configure the Maven Repository

In the Nexus UI you can create a new Maven repository. After creating the repository, you can upload your company’s internal dependencies, plugins, and build artifacts.

2. Configure Maven to Use an Internal Repository

Configuring Maven to use an internal repository requires modifying the settings.xml file.

2.1 Modify settings.xml

In Maven’s settings.xml file, configure the address of the private repository and the authentication information. The settings.xml file is usually located in the ~/.m2/ directory (user-level configuration) or in the ${MAVEN_HOME}/conf/ directory (global configuration).

The following is an example of configuring an internal Maven repository:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<mirrors>
<!-- Configure the private repository mirror -->
<mirror>
<id>nexus</id>
<mirrorOf>external:http://central</mirrorOf>
<url>http://your-nexus-server:8081/repository/maven-public/</url>
<blocked>false</blocked>
</mirror>
</mirrors>

<repositories>
<repository>
<id>internal-repo</id>
<url>http://your-nexus-server:8081/repository/maven-releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>internal-plugins</id>
<url>http://your-nexus-server:8081/repository/maven-plugins/</url>
</pluginRepository>
</pluginRepositories>

<servers>
<!-- Configure Maven repository authentication -->
<server>
<id>nexus</id>
<username>your-nexus-username</username>
<password>your-nexus-password</password>
</server>
</servers>
</settings>

2.2 Configure Mirrors and Repositories

  • Mirror: Configure the URL of the private repository in the tag to proxy Maven’s central repository or other public repository requests to the private repository. Use the mirrorOf configuration to choose which repositories to proxy (external:http://central means proxying all external repositories).

  • Repository: Configure the URL of your internal repository in the and tags.

  • Server Authentication: Configure the authentication information for the internal repository in the tag (if the repository requires authentication).

3. Configure the Project to Use the Internal Repository

In the project’s pom.xml file, you usually do not need to configure repositories separately, because Maven will use the internal repository configured in settings.xml. However, if you need to force a specific repository, you can configure the tag in pom.xml:

1
2
3
4
5
6
<repositories>
<repository>
<id>nexus-repo</id>
<url>http://your-nexus-server:8081/repository/maven-releases/</url>
</repository>
</repositories>

4. Upload and Download Dependencies

4.1 Upload Dependencies to the Internal Repository
You can upload locally built JAR files to the internal repository with Maven commands. For example, to upload a JAR to Nexus:

1
2
3
4
5
6
7
8
mvn deploy:deploy-file \
-DgroupId=com.example \
-DartifactId=my-artifact \
-Dversion=1.0.0 \
-Dpackaging=jar \
-Dfile=path/to/your-artifact.jar \
-DrepositoryId=nexus \
-Durl=http://your-nexus-server:8081/repository/maven-releases/

4.2 Download Dependencies from the Internal Repository

Once the internal repository is configured, Maven will automatically download dependencies from it. If the dependency is not in the repository, Maven will try to download it from other configured repositories.

5. Use Dependencies from the Private Repository

Once the repository is configured, Maven will download dependencies from the configured internal repository. You can reference dependencies normally in the project’s pom.xml, and Maven will automatically pull them from the private repository.

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

6. Configure Mirrors to Improve Build Speed (Optional)

To improve build speed, you can configure settings.xml to use the private repository as Maven’s default mirror, ensuring that all build dependencies are pulled from the private repository, avoiding external repository access on every build and reducing build time.