<settings><localRepository>...</localRepository><servers><server>...</server><servers><mirrors><mirror>...</mirror></mirrors><profiles><profile>...</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-nexus</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-nexus</id> <!-- 与 server 的 id 必须相同,这样才能关联 --><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: 指定当前激活的 profileID 关联关系:1. server.id 与 repository.id 必须相同2. server.id 与 pluginRepository.id 必须相同3. mirror.id 只需要在 mirrors 范围内唯一,通过 mirrorOf 属性匹配仓库4. profile.id 只需要在 profiles 范围内唯一,通过 activeProfile 引用工作原理流程:1.Profile 激活: activeProfile 激活对应的 profile2.仓库解析:从激活的 profile 中获取 repository 和 pluginRepository 中声明的仓库3.认证匹配:如果仓库需要认证,根据 repository.id 和 pluginRepository.id 查找对应的 server.id4.仓库匹配:根据 mirrorOf 规则匹配仓库5.请求重定向:将原仓库请求转发到镜像地址6.依赖下载:从镜像仓库下载所需依赖总结:activeProfile -> profile(环境) -> repository/pluginRepository(仓库) -> server(私服仓库认证) -> mirror(具体访问地址)Maven 构建完整工作流程1. 构建启动阶段(1) 执行 mvn package 命令(2) Maven 读取 settings.xml 配置文件(3) 解析并激活 activeProfiles 中指定的 profile2. 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: 匹配所有仓库,但排除 repo1external:*,!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 中定义的 repositoriessettings.xml 中 profile 定义的 repositories默认的中央仓库 central镜像重定向触发 : 当 Maven 确定要在某个仓库(如 central)下载依赖时,才会应用 mirrorOf 规则进行重定向。 -->