Maven repositories are used to store and manage various artifacts generated during the build process, including dependencies, plugins, and project artifacts. Maven repositories are divided into three types: local repository, central repository, and remote repository.
1. Local Repository:
- Location: Default in the
.m2/repositoryfolder under the user directory - Purpose: Store dependencies downloaded from remote repositories and project build artifacts
- Configuration: Customize location through
<localRepository>tag insettings.xml - Advantage: Avoid repeated downloads, improve build speed, support offline builds
2. Central Repository:
- URL: https://repo.maven.apache.org/maven2
- Purpose: Public repository maintained by Maven official, containing a large number of open source projects
- Features: Can be used without configuration, maintained by Maven community
- Limitation: Projects uploaded to the central repository need to meet strict requirements
3. Remote Repository:
- Purpose: Private repositories built by enterprises or teams, or third-party public repositories
- Common implementations: Nexus, Artifactory, Archiva
- Configuration method:
xml<repositories> <repository> <id>company-repo</id> <url>https://repo.company.com/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
Repository Search Order:
- Local repository: First check if the required artifact already exists locally
- Remote repositories: Search remote repositories in the configured order
- Central repository: If remote repositories cannot find it, finally search the central repository
Mirror Configuration: Mirrors are used to replace remote repositories, usually for accelerating downloads or using enterprise internal repositories:
xml<mirrors> <mirror> <id>aliyun-maven</id> <mirrorOf>central</mirrorOf> <name>Aliyun Maven Mirror</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors>
Repository Authentication Configuration:
For private repositories requiring authentication, configure in settings.xml:
xml<servers> <server> <id>company-repo</id> <username>admin</username> <password>password</password> </server> </servers>
Best Practices:
- Use domestic mirrors to accelerate central repository access
- Build private repositories within enterprises to manage internal dependencies
- Distinguish between releases and snapshots repositories to avoid unstable version pollution
- Regularly clean the local repository to free up disk space
- Use
mvn dependency:purge-local-repositoryto clean local dependencies