在 Maven 中,mirror(镜像)和 repository(仓库)虽然都与下载依赖有关,但它们的角色和使用场景是 完全不同 的。下面详细解释:


🧩 一、基本定义

概念定义作用
repository(仓库)存放 JAR 包、POM 文件、插件等构件的真实位置是依赖下载的源头
mirror(镜像)原始仓库的代理地址(通常速度更快)替代或覆盖原仓库的访问地址,提高下载速度或解决访问限制

📦 二、repository 仓库详解

Maven 默认使用以下几个仓库:

1. 本地仓库(Local Repository)

  • 位置:~/.m2/repository
  • Maven 项目构建时,会优先从本地仓库查找依赖
  • 可通过 settings.xml 修改本地仓库地址
<localRepository>/path/to/local/repo</localRepository>

2. 远程仓库(Remote Repository)

<repositories>
  <repository>
    <id>my-repo</id>
    <url>https://my-repo.com/repo</url>
  </repository>
</repositories>

🪞 三、mirror 镜像详解

mirror 是针对仓库的一种“代理替代”。它通常在 settings.xml 中设置,用于:

  • 替代访问速度慢的中央仓库
  • 指向公司内部私服 Nexus / Artifactory
  • 屏蔽某些不安全的仓库

典型配置示例:

<mirrors>
  <mirror>
    <id>aliyunmaven</id>
    <mirrorOf>central</mirrorOf>
    <name>Aliyun Maven</name>
    <url>https://maven.aliyun.com/repository/central</url>
  </mirror>
</mirrors>

关键字段解释:

字段说明
id镜像 ID,自定义即可
mirrorOf指定要替代的仓库,一般填 central
url镜像地址(实际下载地址)

🔄 四、关系与区别总结

项目repositorymirror
本质真实的依赖存储仓库替代某个仓库的“替身”
配置位置pom.xml 或 settings.xml仅在 settings.xml 中
示例https://repo1.maven.org/maven2https://maven.aliyun.com/
用途下载、上传依赖优化下载速度、规避网络问题
替代关系原始依赖源可自动替代原始仓库

💡 使用建议

  • 日常开发:使用镜像(mirror)提升速度,如阿里云镜像
  • 公司私服:配置 repository 指定具体私有库
  • 发布项目:上传到中央仓库或企业私库时使用 repository

以下是一个完整的 settings.xml 示例,包含:

  1. ✅ 设置本地仓库地址
  2. 🪞 配置阿里云中央仓库镜像
  3. 🔐 添加公司私有仓库凭证
  4. 🔄 激活构建 profile 自动应用私服配置

📄 settings.xml 示例(适用于 Maven 3.x)

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
                              http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <!-- 1. 本地仓库地址 -->
  <localRepository>/Users/yourname/.m2/repository</localRepository>

  <!-- 2. 配置镜像(阿里云 Maven 中央仓库) -->
  <mirrors>
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>central</mirrorOf>
      <name>Aliyun Central Mirror</name>
      <url>https://maven.aliyun.com/repository/central</url>
    </mirror>
  </mirrors>

  <!-- 3. 公司私服凭证(如 Nexus、Artifactory) -->
  <servers>
    <server>
      <id>company-nexus</id>
      <username>your-username</username>
      <password>your-password</password>
    </server>
  </servers>

  <!-- 4. 构建 profile 中设置私服仓库 -->
  <profiles>
    <profile>
      <id>use-company-repo</id>
      <repositories>
        <repository>
          <id>company-nexus</id>
          <name>Company Nexus Repo</name>
          <url>https://nexus.company.com/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <!-- 激活 profile -->
  <activeProfiles>
    <activeProfile>use-company-repo</activeProfile>
  </activeProfiles>

</settings>

✅ 使用说明

  • 将此文件保存为:
    ~/.m2/settings.xml(macOS/Linux)
    C:\Users\你的用户名\.m2\settings.xml(Windows)
  • 请根据实际修改以下部分:
    • yourname:你的系统用户名
    • your-username 和 your-password:私服登录凭据
    • https://nexus.company.com/...:私服地址