Maven Repository
In Maven terminology, a repository is a place.
A Maven repository contains the third-party libraries that a project depends on; the location where these libraries are stored is called a repository.
In Maven, any dependency, plugin, or output of a project build can be called an artifact.
Maven repositories help us manage artifacts (mainly JARs); they are the place where all JAR files (WAR, ZIP, POM, etc.) are stored.
Maven repository types:
| Repository Type | Storage Location | Purpose | Access Priority |
|---|---|---|---|
| Local Repository | Under the user's home directory.m2/repository | Cache dependencies downloaded from remote repositories | 1 |
| Remote Repository | Network server | Provide centralized storage for dependencies and plugins | 2 |
| - Central Repository | repo.maven.apache.org | The default repository maintained by the Maven team | 2.1 |
| - Private Server Repository | Set up within a company (e.g., Nexus) | Host private dependencies and speed up builds | 2.2 |
| - Other public repositories | Such as Aliyun, JCenter, etc. | Mirror or supplement the Central Repository | 2.3 |
Local Repository
Maven's local repository is not created after installing Maven; it is created when the first Maven command is executed.
When running Maven, any artifacts Maven needs are fetched directly from the local repository. If the local repository does not have them, it will first try to download the artifacts from a remote repository to the local repository, and then use the artifacts from the local repository.
By default, whether on Linux or Windows, every user has a repository directory named .m2/repository/ under their home directory.
Windows:
C:\Users\<用户名>\.m2\repositoryLinux/macOS:
~/.m2/repository
The Maven local repository is created under %USER_HOME% by default. To change the default location, define another path in the settings.xml file located in the %M2_HOME%\conf directory.
When you run a Maven command, Maven will download the dependency files to the path you specified.
Clean the local repository:
mvn dependency:purge-local-repository # 或手动删除~/.m2/repository目录
Central Repository
The Maven Central Repository is a repository provided by the Maven community and contains a large number of commonly used libraries.
The Central Repository contains most popular open-source Java artifacts, as well as source code, author information, SCM information, license information, etc. Generally, artifacts that simple Java projects depend on can be downloaded here.
Key concepts of the Central Repository:
- This repository is managed by the Maven community.
- No configuration is required.
- It must be accessed over the network.
Default address of the Central Repository (no explicit configuration required):https://repo.maven.apache.org/maven2/
Remote Repository
Custom Repository Configuration
If Maven cannot find the dependency files in the Central Repository, it will stop the build process and output an error message to the console. To avoid this, Maven provides the concept of remote repositories, which are custom repositories defined by developers, containing the required code libraries or JAR files used in other projects.
For example, using the following pom.xml, Maven will download the dependency files declared in the pom.xml (which are not available in the Central Repository) from a remote repository.
Example
<repository>
<id>aliyun</id>
<name>Aliyun Maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled> <!-- Disable SNAPSHOT versions -->
</snapshots>
</repository>
</repositories>
Mirror Repository (Recommended)
Configure in settings.xml:
Example
<mirror>
<id>aliyun</id>
<name>Aliyun Mirror</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>central</mirrorOf> <!-- Override the Central Repository -->
</mirror>
</mirrors>
Maven Aliyun Repository
The default Maven repository is located overseas, so it is inevitably slow to use in China. We can switch to Aliyun's repository.
Modify the settings.xml file in the conf folder under the Maven root directory, and add the following content to the mirrors node:
If you want to use other proxy repositories, you can<repositories></repositories>add the corresponding repository URLs in the <repositories> node. Take the Spring proxy repository as an example:
In your pom.xml file,<denpendencies></denpendencies>add the file information you want to reference in the <dependencies> node:
Run the fetch command:
mvn install
Gradle Configuration Guide
Add the following code to the build.gradle file:
If you want to use other proxy repositories, take the Spring repository as an example, the code is as follows:
Add the file information you want to reference:
Run the following command to install dependencies:
gradle dependencies 或 ./gradlew dependencies
Private Server Repository
Repository types:
| Repository Type | Purpose |
|---|---|
| hosted | Store private artifacts |
| proxy | Proxy remote repositories |
| group | Aggregate multiple repositories |
Publish to Private Server
Configure in pom.xml:
<distributionManagement>
<repository>
<id>nexus-releases</id>
<url>http://your-nexus/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://your-nexus/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
Configure authentication in settings.xml:
<servers>
<server>
<id>nexus-releases</id>
<username>deploy-user</username>
<password>deploy-pwd</password>
</server>
</servers>Publish command:
mvn deploy
Maven Dependency Search Order
When we execute a Maven build command, Maven searches for dependency libraries in the following order:
- Step 1- Search the local repository. If not found, go to Step 2; if found, proceed with other operations.
- Step 2- Search the Central Repository. If not found and one or more remote repositories have been configured, go to Step 4; if found, download it to the local repository for future reference.
- Step 3- If no remote repositories are configured, Maven will simply halt processing and throw an error (unable to find the dependency files).
- 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 (unable to find the dependency files).
Common Commands
| Command | Purpose |
|---|---|
mvn dependency:resolve | Resolve dependencies |
mvn dependency:tree | View the dependency tree |
mvn dependency:purge-local-repository | Clean the local repository |
mvn deploy | Publish to a remote repository |
Common Issues and Solutions
1. Symptom: dependency download failure:Could not transfer artifact...
Solution:- Check the network connection
- Verify whether the repository URL is correct
- Temporarily disable the firewall to test
2. Symptom: authentication failure:401 Unauthorized
Solution:
- Check the <server> configuration in settings.xml
- Confirm whether the username/password has repository access permissions
3. Dependency conflicts
Troubleshooting:
mvn dependency:tree -Dverbose
Solution: Unify the version in <dependencyManagement>
Other Extensions