Maven settings.xml profiles profile repositories 仓库配置

5次阅读

profile不生效主因是未激活,需在settings.xml的activeprofiles中声明或用-p参数;repositories和pluginrepositories须分开配置;profile中repository优先级高于pom.xml,且不影响mirrors。

Maven settings.xml profiles profile repositories 仓库配置

settings.xml 里 profile 不生效?先检查 activeProfiles

绝大多数 profile 配置失败,不是写错了,而是没激活。maven 默认不自动启用任何 profile,哪怕它定义在 settings.xml 里。

必须显式声明激活方式:

  • settings.xml<activeprofiles></activeprofiles> 块中添加对应 <activeprofile>your-profile-id</activeprofile>
  • 或启动时加参数:mvn clean install -P your-profile-id
  • 注意:profileid 必须严格匹配,区分大小写,且不能含空格或特殊字符

repositoriespluginRepositories 必须分开配

很多人只配了 <repositories></repositories>,结果 mvn archetype:generate 还是拉不到插件——因为 archetype 是插件,走的是 pluginRepositories 路径。

典型错误配置:

<profile>   <id>nexus</id>   <repositories>     <repository>       <id>nexus-repo</id>       <url>https://nexus.example.com/repository/maven-public/</url>     </repository>   </repositories> </profile>

正确做法是补上 pluginRepositories,且 idurl 通常要一致(尤其私有 Nexus):

  • <pluginrepositories></pluginrepositories> 块不能省,内容结构和 <repositories></repositories> 几乎一样
  • 如果用 Nexus,<pluginrepository></pluginrepository>url 一般和 <repository></repository> 相同,除非你明确分开了插件仓库
  • 别漏掉 <releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots>,否则某些仓库会拒绝 SNAPSHOT 依赖

profile 中的 repository 优先级高于 pom.xml 里的同名配置

这是关键行为:一旦 profile 激活,它定义的 <repository></repository> 会覆盖项目 pom.xml 中同 id 的仓库地址,包括镜像逻辑。

常见踩坑点:

  • 你的 pom.xml 写了 <repository><id>central</id><url>https://repo1.maven.org/maven2/</url></repository>,但 profile 里也定义了 <id>central</id> 指向内网 Nexus —— Maven 就不会再去连中央仓库
  • 如果 profile 里没写 <id>central</id>,而只写了自定义 id,那 pom.xmlcentral 仍有效,但会被 mirrors 截流(见下一点)
  • profile 的 repository 不影响 mirrors 配置;如果 mirrorscentral 映射到内网地址,profile 里再写一遍 central 就多余,还可能冲突

mirrorsprofiles 别混着用

镜像(<mirror></mirror>)是全局重定向,profile 是条件加载;两者机制不同,强行套用容易失效。

比如想“只在测试环境用内网仓库”,有人在 profile 里配 <mirror></mirror> —— 这不行,<mirror></mirror> 只能放在 <mirrors></mirrors> 根节点下,profile 里不识别。

  • 要按环境切仓库,必须用 profile + <repositories></repositories>,而不是镜像
  • <mirror></mirror><mirrorof></mirrorof> 值要谨慎:写 * 会拦截所有仓库,包括 profile 里新加的;推荐用 external:* 或具体 id 列表
  • profile 激活后,它的 <repositories></repositories> 是直接加入仓库列表的,不受 <mirrorof></mirrorof> 控制——除非你把 <mirrorof></mirrorof> 设成 *

实际配置时最麻烦的,是 profile、mirror、pom 三层仓库定义之间的隐式覆盖关系。改一处,得同步核对另外两处是否还在按预期工作。

text=ZqhQzanResources