乐闻世界logo
搜索文章和话题

What are the types of Maven repositories? How to configure private repositories and mirrors?

2月18日 21:34

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/repository folder under the user directory
  • Purpose: Store dependencies downloaded from remote repositories and project build artifacts
  • Configuration: Customize location through <localRepository> tag in settings.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:

  1. Local repository: First check if the required artifact already exists locally
  2. Remote repositories: Search remote repositories in the configured order
  3. 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-repository to clean local dependencies
标签:Maven