网上一大堆,关于maven配置本地私服和镜像的流程,神乎其乎,初学者总是很难理解其中的真正原理,
我通过这篇文章来终结他们;
maven安装文件setting.xml的结构
interactiveMode: 是否与用户开启交互模式,默认为 true
offline: 离线模式,默认为 false
localRepository: 配置本地存储库的位置,默认为${user.home}/.m2/repository
proxies: 代理配置
profiles: 配置环境
activeProfiles: 配置默认激活的环境
pluginGroups: 比如
servers: 配置私服的用户名和密码
mirrors: mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
配置镜像
因为中央仓库在国外,下载比较慢,所以可以配置为定向到阿里云镜像,阿里云镜像里面一般都很全
Nexus-aliyun
central
Nexus aliyun
https://maven.aliyun.com/repository/central
这样访问中央仓库时就会定位到阿里云,不再访问中央仓库。如果阿里云仓库没有呢?怎么办?这种情况一般不会出现,因为阿里云仓库的jar很全
如果你希望如果在阿里云镜像找不到资源时也可以访问问中央仓库,那么阿里云镜像就不能使用
如
关于mirrorOf,如果有同名的mirrorOf,那么只会匹配第一个,
如果有
配置profile
nexus-mr
nexus
http://172.16.8.6:8082/repository/maven-public/
true
true
central
https://maven.aliyun.com/repository/central
true
true
nexus
http://172.16.8.6:8082/repository/maven-public/
true
true
central
https://maven.aliyun.com/repository/central
true
true
配置activeProfiles
用于激活前面的环境配置
nexus-mr
完整的setting.xml配置如下:
D:\m2\repository
org.sonarsource.scanner.maven
maven_3rd_party
maven
maven
maven-releases
maven
maven
maven-snapshots
maven
maven
Nexus-aliyun
central
Nexus aliyun
https://maven.aliyun.com/repository/central
nexus-mr
nexus
http://172.16.8.6:8082/repository/maven-public/
true
true
central
https://maven.aliyun.com/repository/central
true
true
nexus
http://172.16.8.6:8082/repository/maven-public/
true
true
central
https://maven.aliyun.com/repository/central
true
true
jdk-1.8
true
1.8
1.8
1.8
1.8
nexus-mr
注意:此文中虽然mirrorOf和profile中central都同时配置了,也可以无需都配置,二者有一个配置了就可以了
如果你想把私服nexus也配置成mirrorOf也可以,但mirrorOf不可配置为*,否则全部请求都走nexus了,不会走central
测试,我们测试一下是否走了阿里云镜像
项目中增加一个新的pom
org.springframework.cloud
spring-cloud-starter-alibaba-sentinel
0.2.1.RELEASE
更新maven,到本地下载的
m2e-lastUpdated.properties文件中查看下载信息,
内容为
nexus|http\://172.16.8.6\:8082/repository/maven-public/|sources=1628230775917
Nexus-aliyun|https\://maven.aliyun.com/repository/central|sources=1628230775917
证明maven访问仓库顺序为:172.16.8.6(nexus)、 maven.aliyun.com(阿里云镜像)
我们将上面的mirrorOf和profile中的central对应的配置都注释,删除本地jar,再次更新项目maven,内容变成:
nexus|http\://172.16.8.6\:8082/repository/maven-public/|javadoc=1628217742198
nexus|http\://172.16.8.6\:8082/repository/maven-public/|sources=1628217716685
central|https\://repo.maven.apache.org/maven2|javadoc=1628217742198
central|https\://repo.maven.apache.org/maven2|sources=1628217716685
证明maven访问仓库顺序为:172.16.8.6(nexus)、repo.maven.apache.org(默认中央仓库)