English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Maven Repository

In Maven terminology, a repository is a location (place).

The Maven repository is the third-party library of the project's dependencies, and the location of this library is called the repository.

In Maven, any dependency, plugin, or project build output can be called a component.

Maven repositories can help us manage components (mainly JARs), which is the place where all JAR files (WAR, ZIP, POM, etc.) are placed.

Maven repositories have three types:

  • Local (local)

  • Central (central)

  • Remote (remote)

Local Repository

The local repository for Maven is not created after the installation, it is created when the maven command is executed for the first time.

When running Maven, any component Maven needs is directly obtained from the local repository. If the local repository does not have it, it will first try to download the component to the local repository from the remote repository, and then use the component from the local repository.

By default, whether on Linux or Windows, each user has a path named .m2/respository/ directory of the repository.

The default local repository for Maven is created in the %USER_HOME% directory. To change the default location, in the %M2The settings.xml file in the _HOME%\conf directory defines another path.

<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">
      <localRepository>C:/MyLocalRepository</localRepository>
</settings>

When you run Maven commands, Maven will download the dependent files to the specified path.

Central Repository

The Maven Central Repository is provided by the Maven community and contains a large number of commonly used libraries.

The central repository contains the vast majority of popular open-source Java components, as well as source code, author information, SCM, information, license information, etc. Generally, simple Java project components can be downloaded here.

Key concepts of the central repository:

  • This repository is managed by the Maven community.

  • No configuration required.

  • Access requires network.

To browse the contents of the central repository, the Maven community provides a URL:http://search.maven.org/#browse.Using this repository, developers can search for all available code libraries.

Remote repositories

If Maven cannot find the dependent files in the central repository either, it will stop the build process and output error information to the console. To avoid this situation, Maven provides the concept of remote repositories, which are customized repositories for developers, containing the necessary code libraries or jar files used in other projects.

For example, using the following pom.xml, Maven will download the dependent files declared in this pom.xml from the remote repository.

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <dependencies>
      <dependency>
         <groupId>com.companyname.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>companyname.lib1</id>
         <url>http://download.companyname.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.companyname.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>

Maven dependency search order

When we execute the Maven build command, Maven starts to search for the dependent libraries in the following order:

  • Step 1 - Search in the local repository, if not found, execute step 2If found, execute other operations.

  • Step 2 - Search in the central repository. If not found, and one or more remote repositories have been set, then execute step 4If found, download them to the local repository for future reference.

  • Step 3 - If the remote repository has not been set, Maven will simply stop processing and throw an error (cannot find the dependency file).

  • Step 4 - Search for the dependency files in one or more remote repositories. If found, download them to the local repository for future reference. Otherwise, Maven will stop processing and throw an error (cannot find the dependency file).

Maven Aliyun (Aliyun) Repository

Maven repository is located overseas by default, which may be slow in China. We can change it to the Aliyun repository.

Modify the settings.xml file in the conf folder under the Maven root directory, and add the following content to the mirrors section:

<mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun Maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
</mirrors>

Second Step: Add the following to the pom.xml file:

<repositories>  
        <repository>  
            <id>alimaven</id>  
            <name>aliyun Maven</name>  
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
            <releases>  
                <enabled>true</enabled>  
            </releases>  
            <snapshots>  
                <enabled>false</enabled>  
            </snapshots>  
        </repository>  
</repositories>