概述
在java
开发中,经常用maven
来管理依赖。
从maven
私有仓库获取依赖时,需要配置maven
的setting.xml
其中就会需要配置setting.xml
中profile
标签下的私服的repository
信息.
而且因为maven
中央仓库下载依赖太慢,网上一般建议配置阿里等国内的镜像地址.
maven
仓库优先级:
- 本地仓库(
localRepositories
) profile
中的repositories
仓库POM中的repository
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>