好的,简单明了地帮你区分一下 Maven 的 mirror(镜像) 和 repository(仓库)


Maven Repository(仓库)

  • 定义:存放 Maven 构建依赖包(JAR、POM等)的实际位置。
  • 类型
    • 本地仓库(Local Repository):存储在开发者本地机器,默认路径是 ~/.m2/repository
    • 远程仓库(Remote Repository):托管在网络上的仓库,比如 Maven Central、阿里云仓库等。
  • 作用:Maven 通过仓库下载、上传依赖包,进行构建。

Maven Mirror(镜像)

  • 定义:对某个远程仓库的代理或替代,镜像了该仓库的内容。
  • 作用
    • 提供更快、更稳定的访问速度(尤其针对官方仓库被墙或访问缓慢的情况)。
    • 统一访问入口,方便公司内部管理依赖。
  • 配置位置:一般在 settings.xml 中配置 <mirrors> 标签,指定某个仓库的镜像 URL。
  • 原理:当 Maven 请求某个仓库时,如果配置了镜像,会自动改为请求镜像地址。

关系总结

项目说明
Repository真正存放依赖的地方,官方仓库或私服
MirrorRepository 的代理或镜像,替代仓库访问

举例说明

  • 你项目里依赖 junit,Maven 默认从中央仓库(https://repo.maven.apache.org/maven2)下载。
  • 你配置了阿里云镜像(https://maven.aliyun.com/repository/public)作为中央仓库的镜像。
  • 那么 Maven 实际请求的就是阿里云镜像地址,镜像再从中央仓库同步依赖。

下面给你一个完整的 Maven repository 和 mirror 配置示例,以及详细解释如何在 settings.xml 中配置镜像来替代默认仓库访问。


Maven Repository 和 Mirror 配置示例


1. Repository(仓库)配置示例

仓库一般配置在项目的 pom.xml 中,告诉 Maven 依赖从哪里下载。

<repositories>
  <repository>
    <id>central</id>
    <name>Maven Central Repository</name>
    <url>https://repo.maven.apache.org/maven2</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>

  <repository>
    <id>my-company-repo</id>
    <name>Company Internal Repository</name>
    <url>https://repo.mycompany.com/maven2</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>
  • 这里定义了两个仓库,Maven 会按顺序查找依赖。
  • 这些配置影响具体项目,属于项目级别配置。

2. Mirror(镜像)配置示例

镜像一般配置在用户的 Maven 全局配置文件 settings.xml 中,位于 ~/.m2/settings.xml

<settings>
  <mirrors>
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>central</mirrorOf>
      <name>Aliyun Maven Mirror</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
    
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Company Nexus Mirror</name>
      <url>https://repo.mycompany.com/repository/maven-public/</url>
    </mirror>
  </mirrors>
</settings>

解释:

  • <mirrorOf> 指定该镜像代理哪些仓库。
    • central 表示只镜像中央仓库。
    • * 表示代理所有仓库请求。
  • 当 Maven 请求某个仓库的依赖时,会先判断是否有镜像代理这个仓库。
  • 如果匹配,则使用镜像地址替代原仓库地址。

3. 两者作用对比

方面Repository(仓库)Mirror(镜像)
配置位置pom.xml 或 settings.xml 中 <repositories>settings.xml 中 <mirrors>
作用指定依赖实际下载地址替换访问指定仓库,提供代理服务
作用范围项目级(或全局)用户全局(针对所有项目生效)
访问方式Maven 直接访问仓库地址Maven 访问镜像地址,镜像帮忙同步仓库内容
典型用途自定义依赖源,比如公司私服或第三方库解决网络不畅、加速依赖下载,屏蔽官方仓库

4. 常见场景示例

  • 你在中国大陆地区开发,访问 Maven Central 慢或失败,配置阿里云镜像代理 Central 仓库,提高下载速度。
  • 公司搭建了 Nexus 私服,作为所有依赖的统一入口,配置镜像代理所有仓库请求到私服。
  • 项目需要使用私服仓库,配置 <repositories> 指定该仓库。

明白了!下面我为你准备了以下内容:

  1. 阿里云镜像完整 settings.xml 配置示例
  2. 公司私服和公共仓库混合的最佳实践配置示例
  3. <mirrorOf> 属性详细用法讲解

1. 阿里云镜像完整 settings.xml 示例

<?xml version="1.0" encoding="UTF-8"?>
<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
          https://maven.apache.org/xsd/settings-1.0.0.xsd">

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

  <profiles>
    <profile>
      <id>default-profile</id>
      <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>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>default-profile</activeProfile>
  </activeProfiles>
</settings>

这个配置会自动将所有访问 Maven Central 的请求转向阿里云镜像,大幅提升访问速度。


2. 公司私服和公共仓库混合最佳实践

假设公司有私服 Nexus,做为所有仓库的代理和管理中心:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <mirrors>
    <!-- 代理所有仓库请求,指向公司 Nexus 私服 -->
    <mirror>
      <id>company-nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Company Nexus Mirror</name>
      <url>https://repo.mycompany.com/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>company-profile</id>
      <repositories>
        <!-- 私服仓库 -->
        <repository>
          <id>company-releases</id>
          <url>https://repo.mycompany.com/repository/maven-releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>

        <!-- 快照仓库 -->
        <repository>
          <id>company-snapshots</id>
          <url>https://repo.mycompany.com/repository/maven-snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>company-profile</activeProfile>
  </activeProfiles>
</settings>

说明:

  • <mirrorOf>*</mirrorOf> 表示代理所有仓库的请求,包括中央仓库和自定义仓库。
  • Nexus 统一管理依赖和版本,方便权限控制和缓存。
  • 在 pom.xml 里可以再添加私服仓库,或者只用私服的镜像。

3. <mirrorOf> 属性详细说明

mirrorOf 值说明
*代理所有仓库请求,包括中央仓库、私服仓库等。
central仅代理 Maven 中央仓库。
external:*代理所有外部仓库,不包括本地仓库或私服。
repo1,repo2代理指定的仓库(以逗号分隔)。
!repoX排除指定的仓库 repoX,其他仓库都代理。
*,!repoX代理所有仓库,排除 repoX 仓库。

示例:代理所有仓库,排除私服仓库

假设你不想镜像私服仓库 company-releases

<mirror>
  <id>aliyun</id>
  <mirrorOf>*,!company-releases</mirrorOf>
  <name>Aliyun Mirror</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>

总结

配置项作用位置使用场景
Repository定义依赖仓库地址pom.xml 或 settings.xml指定项目依赖来源
Mirror替换仓库地址,实现代理或加速settings.xml访问优化、网络加速、统一管理
<mirrorOf>定义镜像代理哪些仓库settings.xml灵活控制镜像范围

下面给你一份整合了阿里云镜像、公司私服混合配置,并且详细注释的完整 settings.xml 示例,适合中国大陆网络环境下使用,也支持私服和公共仓库共存。

<?xml version="1.0" encoding="UTF-8"?>
<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
                              https://maven.apache.org/xsd/settings-1.0.0.xsd">

  <!-- 镜像配置 -->
  <mirrors>
    <!-- 阿里云镜像,代理中央仓库,提升访问速度 -->
    <mirror>
      <id>aliyun-central</id>
      <mirrorOf>central</mirrorOf>
      <name>Aliyun Maven Central Mirror</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

    <!-- 公司 Nexus 私服,代理所有仓库请求 -->
    <mirror>
      <id>company-nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Company Nexus Mirror</name>
      <url>https://repo.mycompany.com/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <!-- 仓库配置 -->
  <profiles>
    <profile>
      <id>default-profile</id>
      <repositories>
        <!-- Maven中央仓库,作为默认依赖来源 -->
        <repository>
          <id>central</id>
          <url>https://repo.maven.apache.org/maven2</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>

        <!-- 公司私服发布仓库 -->
        <repository>
          <id>company-releases</id>
          <url>https://repo.mycompany.com/repository/maven-releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>

        <!-- 公司私服快照仓库 -->
        <repository>
          <id>company-snapshots</id>
          <url>https://repo.mycompany.com/repository/maven-snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <!-- 激活默认配置 -->
  <activeProfiles>
    <activeProfile>default-profile</activeProfile>
  </activeProfiles>

</settings>

使用说明:

  • 当你执行 Maven 构建时,访问中央仓库 central 会被阿里云镜像代理,速度更快。
  • 访问任何仓库请求会被公司 Nexus 私服代理,Nexus 会缓存公共库和私服库的依赖,统一管理。
  • 你项目 pom.xml 中如果有私服仓库配置,也会被 Nexus 镜像接管。
  • 如需排除某个仓库不被镜像,可在 <mirrorOf> 中用 ! 来排除,比如:*,!company-releases