maven的settings.xml
1. 先定位是哪个profile
2. 接着定位是哪些repository,如果没有配置repository,则访问maven默认仓库https://repo.maven.apache.org/maven2
3. 接着如果有mirror,会拦截这个请求,统一重定向到指定的访问地址
把握Maven依赖解析的三个关键阶段:
-
Profile阶段:确定运行环境配置
-
Repository阶段:确定依赖来源位置
-
Mirror阶段:进行地址重定向

1. settings.xml 的核心作用
settings.xml 是 Maven 的全局配置文件,它主要配置的是 Maven 运行环境本身的信息,而不是某个特定项目的信息。这些信息包括:
-
仓库管理器(Repository Manager)的位置:比如你公司内网的 Nexus 或 Artifactory 私服地址。
-
认证信息(Authentication):访问这些仓库所需的用户名和密码。
-
本地仓库路径
-
代理服务器
-
Profile:可以定义一组环境相关的配置,并在不同环境下激活。
2. “拉取”依赖时,settings.xml 做了什么?
当 Maven 需要从远程仓库下载依赖时:
-
仓库地址:Maven 会首先查看项目的
pom.xml中声明的<repositories>。如果没有,则使用settings.xml中配置的镜像(<mirrors>)或者默认的中央仓库。 -
认证:如果访问的仓库需要登录(比如公司的私服),Maven 会在
settings.xml的<servers>部分查找与仓库ID匹配的<server>配置,并使用其中的用户名和密码进行认证。
结论:对于拉取,settings.xml 提供了仓库位置(通过镜像)和访问权限。
3. “推送”部署时,settings.xml 做了什么?
当你执行 mvn deploy 命令,将项目构件(JAR、WAR等)发布到远程仓库时:
-
部署目标地址:这个地址主要在项目的
pom.xml中定义,在<distributionManagement>部分。Maven 根据这里的<repository>(发布版本)和<snapshotRepository>(发布快照)的ID来确定要推送到哪里。 - 认证信息:Maven 拿着
pom.xml中<repository>标签的ID(例如my-company-release-repo),去settings.xml的<servers>部分寻找相同ID的<server>配置,并使用其中的用户名和密码来完成认证和推送。
结论:对于推送,settings.xml 不关注推送到哪个具体的URL(那是 pom.xml 的事),但它绝对关注并负责提供推送所需的认证信息。
<!-- pom.xml --> <distributionManagement> <repository> <id>yl-releases</id> <name>Internal Releases</name> <url>http://${nexus.ip}:${nexus.port}/repository/yl-releases/</url> </repository> <snapshotRepository> <id>yl-snapshots</id> <name>Internal snapshots</name> <url>http://${nexus.ip}:${nexus.port}/repository/yl-snapshots/</url> </snapshotRepository> </distributionManagement>
所以,正确的理解是:
settings.xml 并不区分拉取和推送,它核心负责的是“认证”和“仓库位置解析”(通过镜像)。无论是拉取还是推送,只要涉及需要认证的远程仓库,都需要在 settings.xml 中配置相应的 <server>。而部署的目标地址,则是由项目的 pom.xml 唯一决定的。
4. 解释mirror和repository的区别以及它们的关系
1. Mirror(镜像)
-
作用:替换/代理其他仓库的访问地址
-
行为:当Maven尝试访问某个仓库时,镜像会拦截并重定向到镜像地址
-
配置位置:
<mirrors>标签下 -
关键属性:
<mirrorOf>指定要镜像哪些仓库
2. Repository(仓库)
-
作用:定义一个具体的仓库位置
-
行为:告诉Maven可以去这里查找依赖
-
配置位置:在
<profile>的<repositories>或<pluginRepositories>下 -
关键属性:
<id>和<url>定义仓库身份和位置
工作过程:
-
Maven需要从
repository.id仓库下载依赖 -
镜像配置
<mirrorOf>*</mirrorOf>拦截了这个请求 -
实际下载地址被重定向到mirror.url配置的地址
是否可以省略其中一个?
情况1:只有Mirror,没有Repository ✅ 可行
效果:所有对Maven中央仓库的请求都会被重定向到阿里云镜像。
<settings> <mirrors> <mirror> <id>aliyun-mirror</id> <url>https://maven.aliyun.com/repository/central</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <!-- 不需要在settings.xml中配置repository,使用默认的中央仓库 --> </settings>
情况2:只有Repository,没有Mirror ✅ 可行
效果:Maven会直接访问配置的仓库地址,没有重定向。
<settings> <profiles> <profile> <id>custom-repos</id> <repositories> <repository> <id>company-repo</id> <url>https://nexus.company.com/repository/maven-group/</url> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>custom-repos</activeProfile> </activeProfiles> </settings>
典型使用场景
Mirror的主要用途:
-
加速下载:使用国内镜像替代国外仓库
-
统一入口:公司内部使用统一的私服地址
-
网络代理:解决直接访问某些仓库的网络问题
Repository的主要用途:
-
添加私有仓库:公司内部的私有依赖库
-
第三方仓库:如JBoss、Cloudera等特定仓库
-
快照仓库:专门的快照版本仓库
总结
-
Mirror:是流量转发器,控制"去哪里下载"
-
Repository:是地址定义器,定义"有哪些仓库可用"
是否可以省略:
-
✅ 可以只有Mirror没有Repository(常见)
-
✅ 可以只有Repository没有Mirror(也常见)
-
❌ 但两者都没有的话,就只能用默认中央仓库
在企业环境中,通常推荐使用Mirror来统一管理仓库访问,这样配置更简洁且易于维护。
-
Mirror是流量控制层,追求简洁统一
-
Repository是资源定义层,需要覆盖各种依赖来源
<settings> <localRepository>...</localRepository> <servers> <server>...</server> <servers> <mirrors> <mirror>...</mirror> <!-- 镜像:流量控制层 --> </mirrors> <profiles> <profile>
<id>...</id> <repositories> <repository>...</repository> <!-- 仓库资源定义层:各种依赖来源 --> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>...</activeProfile> <!-- 与 profile.id 相同 --> </activeProfiles> <settings>
<?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 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>D:\develop\maven\maven_repository</localRepository> <!-- servers 标签 --> <!-- 作用:配置访问远程仓库所需的认证信息 --> <!-- 用途:存储用户名、密码、私钥等安全信息 --> <servers> <!-- 公司内部私服认证 --> <server> <id>yl-releases</id> <username>admin</username> <password>123456</password> </server> <!-- 阿里云私服认证 --> <!-- ... --> </servers> <!-- mirrors 标签 --> <!-- 作用:配置仓库镜像,重定向仓库请求 --> <!-- 用途:加速下载、替换默认中央仓库 --> <mirrors> <!-- 镜像私服1 --> <mirror> <id>nexus</id> <!-- mirror 的 id 只需要在 mirrors 范围内唯一 --> <mirrorOf>*</mirrorOf> <!-- mirror 通过 mirrorOf 属性来匹配要重定向的仓库,不依赖 id 匹配 --> <name>yl maven mirror</name> <url>http://192.168.80.158:8099/repository/yl/</url> </mirror> <!-- 镜像私服2 --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven mirror</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors> <!-- profiles 标签 --> <!-- 作用:定义不同的配置环境 --> <!-- 用途:管理不同环境下的仓库、属性、插件等配置 --> <profiles> <!-- 私服1环境 --> <profile> <id>yl</id> <!-- 工作原理: 当 Maven 访问 id 为 yl-releases 的 repository 时,会自动查找 servers 中 id 相同的 server 配置,使用其中的认证信息进行身份验证。 --> <repositories> <!-- 私服1 --> <repository> <id>yl-releases</id> <!-- 1.与 server.id 必须相同,这样才能关联, 2.这个id会被mirror拦截 --> <name>ylmaven nexus repository</name> <url>http://192.168.80.158:8099/repository/yl/</url> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <!-- 插件私服1 --> <pluginRepository> <id>central</id> <name>Nexus</name> <url>http://192.168.80.158:8099/repository/yl/</url> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> </profile> <!-- 公网环境 --> <profile> <id>public-env</id> <repositories> <repository> <id>aliyun-repo</id> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> </profile> </profiles> <!-- activeProfiles 标签 --> <!-- 作用:激活指定的 profile --> <!-- 用途:确定当前使用哪个配置环境 --> <activeProfiles> <activeProfile>yl</activeProfile> <!-- 与 profile.id 相同 --> </activeProfiles> </settings> <!-- 各配置元素的作用: server: 存储访问远程仓库的认证信息(用户名、密码) mirror: 重定向仓库请求到镜像仓库 profile: 定义不同环境的配置集合 repository: 声明项目依赖的仓库地址 pluginRepository: 声明插件依赖的仓库地址 activeProfile: 指定当前激活的 profile ID 关联关系: 1. server.id 与 repository.id 必须相同 2. server.id 与 pluginRepository.id 必须相同 3. mirror.id 只需要在 mirrors 范围内唯一,通过 mirrorOf 属性匹配仓库 4. profile.id 只需要在 profiles 范围内唯一,通过 activeProfile 引用 工作原理流程: 1.Profile 激活: activeProfile 激活对应的 profile 2.仓库解析:从激活的 profile 中获取 repository 和 pluginRepository 中声明的仓库 3.认证匹配:如果仓库需要认证,根据 repository.id 和 pluginRepository.id 查找对应的 server.id 4.仓库匹配:根据 mirrorOf 规则匹配仓库 5.请求重定向:将原仓库请求转发到镜像地址 6.依赖下载:从镜像仓库下载所需依赖 总结:activeProfile -> profile(环境) -> repository/pluginRepository(仓库) -> server(私服仓库认证) -> mirror(具体访问地址) Maven 构建完整工作流程 1. 构建启动阶段 (1) 执行 mvn package 命令 (2) Maven 读取 settings.xml 配置文件 (3) 解析并激活 activeProfiles 中指定的 profile 2. Profile 激活与合并 (1) 根据 activeProfile 激活对应的 profile (2) 将激活的 profile 中的配置与全局配置合并 (3) 获取 profile 中定义的 repository 和 pluginRepository 仓库列表 3. 依赖解析阶段 (1) 解析项目 pom.xml 中声明的依赖 (2) 确定每个依赖所需的仓库位置 4. 镜像匹配与重定向 (1) 检查 mirrors 配置中的 mirrorOf 规则 (2) 根据规则将原始仓库请求重定向到镜像地址 例如:mirrorOf=central 会将中央仓库请求重定向到镜像 5. 仓库认证匹配 (1) 根据 repository.id 查找对应的 server 配置 (2) 获取访问私有仓库所需的认证信息(用户名、密码) 6. 依赖下载 (1) 从重定向后的镜像仓库或原始仓库下载依赖 (2) 如果是私有仓库,则使用对应的认证信息 7. 编译与打包 (1) 基于下载的依赖执行编译 (2) 运行测试(如果包含 test 阶段) (3) 执行打包操作生成最终产物 整个流程中,profile 提供了环境特定的配置,mirror 加速了依赖下载,server 提供了认证支持。 --> <!-- Maven mirrorOf 规则详解 1. 基本规则 *: 匹配所有仓库 external:*: 匹配所有外部仓库(除了本地仓库) central: 仅匹配中央仓库 repo1: 匹配 Maven 1.x 的中央仓库 2. 排除规则 *,!repo1: 匹配所有仓库,但排除 repo1 external:*,!central: 匹配所有外部仓库,但排除中央仓库 3. 多仓库匹配 central,public: 匹配 central 和 public 两个仓库 repo1,repo2,repo3: 匹配多个指定仓库 4. 特殊规则 external:http:*: 匹配所有使用 HTTP 协议的外部仓库 *>external:*: 复杂的排除和包含组合 5. 工作原理 Maven 根据 mirrorOf 的值来决定哪些仓库请求需要被重定向 当多个 mirror 配置匹配同一个仓库时,按照配置顺序优先匹配第一个 匹配成功后,原始仓库的 URL 会被替换为 mirror 的 url --> <!-- Maven 依赖仓库定位机制: (1) 依赖的 groupId、artifactId、version 定位 例如:com.alibaba:fastjson:1.2.78 → /com/alibaba/fastjson/1.2.78/fastjson-1.2.78.jar (2) 仓库搜索顺序 本地仓库:首先检查 ~/.m2/repository/ 配置的远程仓库:按照 settings.xml 和 pom.xml 中定义的仓库顺序搜索 (3) 仓库继承机制 项目 pom.xml 中定义的 repositories 父 pom.xml 中定义的 repositories settings.xml 中 profile 定义的 repositories 默认的中央仓库 central 镜像重定向触发 : 当 Maven 确定要在某个仓库(如 central)下载依赖时,才会应用 mirrorOf 规则进行重定向。 -->
通常的配置
pom.xml
<distributionManagement> <!-- 发布版本部署地址 --> <repository> <id>my-nexus-releases</id> <name>My Nexus Releases</name> <url>http://your-nexus-host:8081/repository/maven-releases/</url> </repository> <!-- 快照版本部署地址 --> <snapshotRepository> <id>my-nexus-snapshots</id> <name>My Nexus Snapshots</name> <url>http://your-nexus-host:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement>
settings.xml
<settings> <!-- Mirror配置:统一入口 --> <mirrors> <mirror> <id>my-nexus</id> <name>My Nexus Repository Group</name> <url>http://localhost:8081/repository/maven-public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> <!-- 服务器认证信息 --> <servers> <server> <id>my-nexus-releases</id> <username>deployment</username> <password>deployment123</password> </server> <server> <id>my-nexus-snapshots</id> <username>deployment</username> <password>deployment123</password> </server> </servers> <!-- Profile配置 --> <profiles> <profile> <id>nexus</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>nexus</activeProfile> </activeProfiles> </settings>

浙公网安备 33010602011771号