中央仓库与镜像配置
中央仓库是 Maven 默认远程仓库,镜像提供加速访问。
中央仓库
默认地址
XML
https://repo.maven.apache.org/maven2
超级 POM 配置
XML
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
镜像配置
配置位置
settings.xml 中的 mirrors 元素:
XML
<settings>
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
</settings>
mirror 元素说明
| 元素 | 说明 |
|---|---|
| id | 镜像唯一标识 |
| mirrorOf | 被镜像的仓库ID |
| name | 镜像名称 |
| url | 镜像地址 |
国内常用镜像
阿里云镜像
XML
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
华为云镜像
XML
<mirror>
<id>huawei</id>
<mirrorOf>central</mirrorOf>
<url>https://repo.huaweicloud.com/repository/maven/</url>
</mirror>
腾讯云镜像
XML
<mirror>
<id>tencent</id>
<mirrorOf>central</mirrorOf>
<url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
</mirror>
mirrorOf 规则
基本规则
| mirrorOf | 说明 |
|---|---|
| central | 仅镜像中央仓库 |
| * | 镜像所有仓库 |
| external:* | 镜像所有非本地仓库 |
| repo1,repo2 | 镜像多个指定仓库 |
| *,!repo1 | 镜像除 repo1 外所有仓库 |
示例配置
XML
<!-- 镜像所有仓库 -->
<mirror>
<id>aliyun</id>
<mirrorOf>*</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 镜像除私服外的所有仓库 -->
<mirror>
<id>aliyun</id>
<mirrorOf>*,!private-repo</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
镜像优先级
XML
请求依赖时:
1. 检查本地仓库
2. 根据镜像配置查找镜像URL
3. 从镜像下载(替代原仓库)
镜像完全替代原仓库请求,不会同时访问原仓库。
多镜像配置
Bash
<mirrors>
<!-- 阿里云镜像 -->
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 公司私服 -->
<mirror>
<id>company</id>
<mirrorOf>company-repo</mirrorOf>
<url>https://nexus.company.com/repository/maven-public/</url>
</mirror>
</mirrors>
插件仓库镜像
text
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
插件仓库默认使用中央仓库,镜像配置同样生效。
验证镜像生效
text
mvn dependency:resolve -X
查看日志中的下载URL:
text
[DEBUG] Using mirror aliyun for central
Downloading from aliyun: https://maven.aliyun.com/repository/public/...
要点总结
- 中央仓库:repo.maven.apache.org/maven2
- 镜像替代原仓库请求,加速下载
- 国内推荐阿里云镜像
- mirrorOf=* 镜像所有仓库
- mirrorOf=*,!repo 排除特定仓库
- 镜像配置在 settings.xml 的 mirrors 元素
📝 发现内容有误?点击此处直接编辑