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>test1</id>
  <mirrorOf>central</mirrorOf>   
  <name>test1</name>
  <url>https://test1.org/maven2/</url>
</mirror>

上面配置了两个中央仓库的镜像,错误的以为阿里云找不到就会去test1找,很遗憾的告诉你,并不会去test1取!
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>test1</id>
  <mirrorOf>*</mirrorOf>   
  <name>test1</name>
  <url>https://test1.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>test1</id>
		<url>https:/test1.org/maven/</url>
	</repository>
	<repository>
		<id>test2</id>
		<url>https://test2.com/releases/</url>
	</repository>
</repositories>

还可以在setting.xml中设置不同环境使用不同的repositories

<profiles>
	<profile>
		<id>dev</id>
		<repositories>
			<repository>
				<id>test1</id>
				<url>https://dev.com/maven/</url>
			</repository>
		</repositories>
	</profile>
		<profile>
		<id>pro</id>
		<repositories>
			<repository>
				<id>test1</id>
				<url>https://pro.com/maven/</url>
			</repository>
		</repositories>
	</profile>
</profiles>

版权声明:本文为qq_27870421原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。