很多人对maven镜像有着错误的理解,以为可以在settings.xml中配置多个,这个镜像下载不下来,可以到另外一个镜像去下载。通常我们在互联网环境开发项目,所有的jar包都需要到maven的中央仓库去取。但是中央仓库的url地址是国外的,下载jar包的速度很慢,这时我们一般都会配置阿里云镜像
aliyun
central
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
或者
aliyun
*
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
如果工程中的jar包都能在阿里镜像中找到,mirrorOf填central还是都是可以的。central表示覆盖maven中央仓库的默认url,表示所有的仓库都到我配置的这个url取。假如这时候工程中需要引入一个jar包,而这个jar包在阿里云找不到,需要到另外一个仓库url去取,这时候有人就会错误的配置镜像
aliyun
central
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
wso2
central
wso2
https://dist.wso2.org/maven2/
上面配置了两个中央仓库的镜像,错误的以为阿里云找不到就会去wso2找,很遗憾的告诉你,想错了!先来看一段maven官方的描述
image.png
根据官方的描述,mirrorOf相同的情况下,仅会选择靠前的镜像,当jar包下载失败后,是不会去第2个镜像下载的。那官方又说了若要使用多个仓库,请改用存储库管理器,什么是存储库管理器?也就是maven私服,这里不展开讲它的作用。然后又有人使用了另外的配置来解决
aliyun
central
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
wso2
*
wso2
https://dist.wso2.org/maven2/
这样虽然解决jar包的问题,但是若此时再引入第3个jar包,该jar包在前面2个仓库中都找不到,那又该如何配置?是不是没辙了!正确的配置是把后面两个特殊jar包的仓库地址配置到pom.xml中,settings.xml中只保留阿里镜像
aliyun
central
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
wso2
https://dist.wso2.org/maven2/
hortonworks
https://repo.hortonworks.com/content/repositories/releases/
接下来谈谈id与mirrorOf的关系,先来一段代码
org.eclipse.emf
org.eclipse.emf.query
1.7.0.201306111332
org.apache.taglibs
taglibs-standard-spec
1.2.5
rep1
https://rep1.com/
rep2
https://rep2.com/
wso2
rep1
wso2
https://dist.wso2.org/maven2/
aliyun
rep2
aliyun
http://maven.aliyun.com/nexus/content/groups/public/
然后maven compile,你会惊奇的发现2个jar包都下载下来了。因此可以发现mirrorOf=id,所谓镜像就是用来重写仓库url的。中央仓库的id=central,id和url都在apache-maven-3.6.3-bin.zip目录中的某个jar包里配着的,具体是哪个jar包,记不得了。所以中央仓库是不需要在工程中配置的。
maven官方文档:http://maven.apache.org/guides/mini/guide-mirror-settings.html
本文暂时没有评论,来添加一个吧(●'◡'●)