让 maven 项目的镜像依次生效,达成 pom 依赖先去第一个镜像寻找依赖,如果找不到,再依次去后面的镜像寻找。
在 maven 的 settings.xml,去配置多镜像,如下面例子所示:
<mirrors>
<mirror>
<id>alimavenid>
<name>aliyun mavenname>
<url> https://maven.aliyun.com/repository/centralurl>
<mirrorOf>centralmirrorOf>
mirror>
<mirror>
<id>centralid>
<name>Maven Repository Switchboardname>
<url>https://repo1.maven.org/maven2/url>
<mirrorOf>centralmirrorOf>
mirror>
<mirror>
<id>aliyunmavenid>
<name>阿里云spring插件仓库name>
<url>https://maven.aliyun.com/repository/spring-pluginurl>
<mirrorOf>centralmirrorOf>
mirror>
<mirror>
<id>repo2id>
<name>Mirror from Maven Repo2name>
<url>https://repo.spring.io/plugins-release/url>
<mirrorOf>centralmirrorOf>
mirror>
mirrors>
该配置设置了 4 个镜像,但镜像生效,是当一个镜像完全不可用,才会依次执行下一个镜像。接着要进行下面配置。
注意:
配置多个mirror时,mirrorOf不能配置*。*的意思就是(根据mirrorOf和repository的id)匹配所有的库(repository),这样就是说如果你需要某个jar,他会从镜像地址去下载这个jar。不管你配置了多少个库,即使这些库的地址不一样,仍然会从镜像地址访问。
<profiles>
<profile>
<id>jdk-1.8id>
<activation>
<activeByDefault>trueactiveByDefault>
<jdk>1.8jdk>
activation>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
properties>
profile>
<profile>
<id>nexusid>
<repositories>
<repository>
<id>centralid>
<url>https://repo1.maven.org/maven2/url>
<snapshots>
<enabled>falseenabled>
snapshots>
<releases>
<enabled>trueenabled>
releases>
repository>
<repository>
<id>alimavenid>
<url>https://maven.aliyun.com/repository/centralurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
<repository>
<id>aliyunmavenid>
<url>https://maven.aliyun.com/repository/spring-pluginurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
<repository>
<id>repo2id>
<url>https://repo.spring.io/plugins-release/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>centralid>
<url>https://repo1.maven.org/maven2/url>
<snapshots>
<enabled>falseenabled>
snapshots>
<releases>
<enabled>trueenabled>
releases>
pluginRepository>
<pluginRepository>
<id>alimavenid>
<url>https://maven.aliyun.com/repository/centralurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
<pluginRepository>
<id>aliyunmavenid>
<url>https://maven.aliyun.com/repository/spring-pluginurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
<pluginRepository>
<id>repo2id>
<url>https://repo.spring.io/plugins-release/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
pluginRepositories>
profile>
profiles>
设置生效的 activeProfiles,指向上面 profile 的 id
<activeProfiles>
<activeProfile>nexusactiveProfile>
activeProfiles>