如何强制Maven Central流量经CodeArtifacts,其余走企业Nexus仓库?
问题分析与解决方案
为什么原配置无效?
这不是Maven的预期行为,问题出在第二个镜像的<mirrorOf>规则错误:
<mirrorOf>*,!aws-codeartifact</mirrorOf>中的!aws-codeartifact是镜像的ID,但Maven的<mirrorOf>排除规则针对的是仓库的ID,而非镜像ID。- 由于没有名为
aws-codeartifact的仓库,这条排除规则不生效,*会匹配所有仓库请求,导致所有流量都被Nexus镜像捕获,覆盖了第一个镜像的规则。
正确配置方案
1. 修正镜像规则
调整第二个镜像的<mirrorOf>为*,!central,明确排除Maven Central仓库(ID为central),确保Central请求走CodeArtifact,其余走Nexus:
<mirrors> <mirror> <id>aws-codeartifact</id> <mirrorOf>central</mirrorOf> <name>CodeArtifact mirror for Maven Central only</name> <url>https://abc-code-artifacts.d.codeartifact.us-east-1.amazonaws.com/maven/maven-test-repo/</url> </mirror> <mirror> <id>abc-nexus</id> <mirrorOf>*,!central</mirrorOf> <name>Nexus Group for everything else</name> <url>https://nexus.abc.com/repository/repository/maven-group/</url> </mirror> </mirrors>
2. 忽略项目pom.xml中的仓库配置
要强制忽略pom里的自定义仓库,需在settings.xml中添加一个默认激活的Profile,覆盖全局仓库配置,只保留Central仓库(其余请求会被镜像转发):
<profiles> <profile> <id>override-project-repos</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>https://repo1.maven.org/maven2</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles>
3. 额外验证
确保AWS CodeArtifact仓库已正确代理Maven Central,否则无法拉取Central的依赖。
内容的提问来源于stack exchange,提问作者Amal Syed
相关产品推荐
相关产品推荐

