概述

在java开发中,经常用maven来管理依赖。

从maven私有仓库获取依赖时,需要配置maven的setting.xml
其中就会需要配置setting.xml中profile标签下的私服的repository信息.

而且因为maven中央仓库下载依赖太慢,网上一般建议配置阿里等国内的镜像地址.

maven仓库优先级:

  1. 本地仓库(localRepositories)
  2. profile中的repositories仓库
  3. POM中的repository
  4. mirrors全局仓库

mirror和repository关系

先说repository,顾名思义,就是仓库的意思,maven的setting.xml中有个默认仓库就是maven中央仓库.
repositories下会有多个仓库,maven会依次请求,第一个不可用则会使用第二个

标识我们下载依赖的时候都从这个仓库去下载,但是国内下载太慢,就会建议mirror中配置上阿里的地址,这里我们可以理解为mirror就像nginx的反向代理,我们去下载依赖的时候,其实是从阿里的镜像仓库去下载的。

这里就要说到这个repository的id和mirror中的mirrorOf标签,这两个是对应的,代表mirror需要代理哪个仓库.
<mirrorOf>*</mirrorOf>代表所有repository仓库都走这个镜像仓库.

配置demo如下

<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <!--mirrorOf的配置很重要后面会详细说明-->
  <mirrorOf>central</mirrorOf>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

<profiles>
	<profile>
		<id>配置文件id</id>
		<repositories>
		  <!--maven默认的内置仓库(maven中央仓库)-->
		  <repository>
		   <id>central</id>
		   <name>Central Repository</name>
		   <url>https://repo.maven.apache.org/maven2</url>
		   <layout>default</layout>
		   <snapshots>
		    <enabled>false</enabled>
		   </snapshots>
		  </repository>
		</repositories>
	<profile>
</profiles>

<activeProfiles>
  <!--默认的配置文件-->
  <activeProfile>配置文件id</activeProfile>
</activeProfiles>

更多推荐