关于Maven Mirrors 的正确使用

很多人对maven镜像有着错误的理解,以为可以在settings.xml中配置多个,这个镜像下载不下来,可以到另外一个镜像去下载。通常我们在互联网环境开发项目,所有的jar包都需要到maven的中央仓库去取。但是中央仓库的url地址是国外的,下载jar包的速度很慢,这时我们一般都会配置阿里云镜像

<mirror>
  <id>aliyun</id>
  <mirrorOf>central</mirrorOf>   
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

或者

<mirror>
  <id>aliyun</id>
  <mirrorOf>*</mirrorOf>   
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

如果工程中的jar包都能在阿里镜像中找到,mirrorOf填central还是*都是可以的。central表示覆盖maven中央仓库的默认url,*表示所有的仓库都到我配置的这个url取。假如这时候工程中需要引入一个jar包,而这个jar包在阿里云找不到,需要到另外一个仓库url去取,这时候有人就会错误的配置镜像

<mirror>
  <id>aliyun</id>
  <mirrorOf>central</mirrorOf>   
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

<mirror>
  <id>wso2</id>
  <mirrorOf>central</mirrorOf>   
  <name>wso2</name>
  <url>https://dist.wso2.org/maven2/</url>
</mirror>

上面配置了两个中央仓库的镜像,错误的以为阿里云找不到就会去wso2找,很遗憾的告诉你,想错了!先来看一段maven官方的描述

 根据官方的描述,mirrorOf相同的情况下,仅会选择靠前的镜像,当jar包下载失败后,是不会去第2个镜像下载的。那官方又说了若要使用多个仓库,请改用存储库管理器,什么是存储库管理器?也就是maven私服,这里不展开讲它的作用。然后又有人使用了另外的配置来解决

<mirror>
  <id>aliyun</id>
  <mirrorOf>central</mirrorOf>   
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

<mirror>
  <id>wso2</id>
  <mirrorOf>*</mirrorOf>   
  <name>wso2</name>
  <url>https://dist.wso2.org/maven2/</url>
</mirror>

这样虽然解决jar包的问题,但是若此时再引入第3个jar包,该jar包在前面2个仓库中都找不到,那又该如何配置?是不是没辙了!正确的配置是把后面两个特殊jar包的仓库地址配置到pom.xml中,settings.xml中只保留阿里镜像

<!-- settings.xml -->
<mirror>
  <id>aliyun</id>
  <mirrorOf>central</mirrorOf>   
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<!-- pom.xml -->
<repositories>
	<repository>
		<id>wso2</id> <!-- id可以随便取,只要不重名即可 -->
		<url>https://dist.wso2.org/maven2/</url>
	</repository>
	<repository>
		<id>hortonworks</id>
		<url>https://repo.hortonworks.com/content/repositories/releases/</url>
	</repository>
</repositories>

接下来谈谈id与mirrorOf的关系,先来一段代码

<!-- pom.xml -->
<dependencies>
	<!-- 该jar包在阿里云和maven默认的中央仓库,都是找不到的 -->
	<dependency>
		<groupId>org.eclipse.emf</groupId>
		<artifactId>org.eclipse.emf.query</artifactId>
		<version>1.7.0.201306111332</version>
	</dependency>
	<dependency>
		<groupId>org.apache.taglibs</groupId>
		<artifactId>taglibs-standard-spec</artifactId>
		<version>1.2.5</version>
	</dependency>
</dependencies>

<!-- url是我乱写的,根本不存在 -->
<repositories>
	<repository>
		<id>rep1</id>
		<url>https://rep1.com/</url>
	</repository>
	<repository>
		<id>rep2</id>
		<url>https://rep2.com/</url>
	</repository>
</repositories>
<!-- settings.xml -->

<!-- id可以随便取,只要不重名即可 -->
<mirror>
  <id>wso2</id>
  <mirrorOf>rep1</mirrorOf>   
  <name>wso2</name>
  <url>https://dist.wso2.org/maven2/</url>
</mirror>
	
<mirror>
  <id>aliyun</id>
  <mirrorOf>rep2</mirrorOf>   
  <name>aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

然后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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值