全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-23 5 分钟 ✍️ juanwangdev

中央仓库与镜像配置

中央仓库是 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 元素

📝 发现内容有误?点击此处直接编辑

← 上一篇 生命周期阶段绑定
下一篇 → 仓库认证与安全
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库