Maven多仓库的优先级
# 一、Maven仓库的分类
# 1. 本地仓库
<localRepository>/D:/ResLibFilesWorkspace/Repository/Maven</localRepository>
# 2. 中央仓库
Maven 必须要知道至少一个可用的远程仓库,中央仓库就是这样一个默认的远程仓库。
中央仓库配置在 Maven 的 super pom 文件,文件为位于 Maven 安装目录里 \lib 下 maven-model-builder-x.x.x.jar 中的 org/apache/maven/model/pom-4.0.0.xml。
··· 省略其他
<repositories>
<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>
···
2
3
4
5
6
7
8
9
10
11
12
13
# 3. Maven镜像
镜像(Mirroring)是冗余的一种类型,一个磁盘上的数据在另一个磁盘上存在一个完全相同的副本即为镜像。
mirrorOf 标签
mirrorOf 标签里面放置的是 repository 配置的 id,为了满足一些复杂的需求,还支持更高级的配置如下:
external:*
,不在本地仓库的文件才从该镜像获取。repo,repo1
, 远程仓库 repo 和 repo1 从该镜像获取。*,!repo1
,所有远程仓库都从该镜像获取,除 repo1 远程仓库以外。*
,所用远程仓库都从该镜像获取。
注意:当远程仓库被 mirror 镜像匹配时,依赖包将从 mirror 镜像仓库获取,而不是配置的 repository 仓库, repository 将失去作用。
# 4. 私有仓库
私服(私有仓库)是一种特殊的远程Maven仓库,它是架设在局域网内的仓库服务,私服一般被配置为互联网远程仓库的镜像,供局域网内的 Maven 用户使用。
当 Maven 需要下载构件的时候,先向私服请求,如果私服上不存在该构件,则从外部的远程仓库下载,同时缓存在私服之上,然后为 Maven 下载请求提供下载服务,另外,对于自定义或第三方的jar可以从本地上传到私服,供局域网内其他maven用户使用。
优点:
- 节省外网宽带。
- 加速Maven构建。
- 部署第三方构件:可以将公司项目的 jar 发布到私服上,方便项目与项目之间的调用。
- 提高稳定性、增强控制:原因是外网不稳定。
- 降低中央仓库的负荷:原因是中央仓库访问量太大。
# 二、Maven仓库的配置
Maven 可以配置位置如下:
- pom.xml 的
repository
- pom.xml 的
profile
的repository
- settings.xml 的
profile
的repository
- settings.xml 的
mirror
排除镜像代理*
的情况,优先级如下:
本地仓库 > 私服 (settingxml.profile)> 远程仓库(pom.xml-repository)和 镜像 (mirror)
# 私服 (settingxml.profile)配置
注意:
- 同在 settings.xml 的 properties,如果都激活了,根据 profile 定义的先后顺序来进行覆盖取值,后面定义的会覆盖前面,并不是根据activeProfile定义的顺序 。
- 如果有user settings(用户目录下的setting)和globel settings(安装目录下的 settings),则两者合并,其中重复的配置,以user settings为准。
示例:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>C:/repository</localRepository>
<servers>
<!-- 私服用户名密码 注意id与下面 profile.repository.id 对应 -->
<server>
<id>lakernexus</id>
<username>laker</username>
<password>laker123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf> <!-- 只代理central中央仓库镜像-->
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<!-- 定义profile注意下面激活的时候使用-->
<id>lakerProfile</id>
<repositories>
<repository>
<!-- server.id对应-->
<id>lakernexus</id>
<url>https://lakernexus.com/repository/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>lakernexus</id>
<url>https://lakernexus.com/repository/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!-- 定义profile注意下面激活的时候使用-->
<activeProfile>lakerProfile</activeProfile>
</activeProfiles>
</settings>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 远程仓库(pom.xml-repository)配置
注意:pom.xml中配置多个 repository,会按照配置的先后顺序从repository1、repository2...下载依赖包,即 repository1 中找不到的情况下才会从 repository2 下载。
示例:
<parent>
......
</parent>
<dependencies>
......
</dependencies>
......
<repositories>
<!-- https://developer.aliyun.com/mvn/guide -->
<repository>
<id>aliyun-public1</id>
<url>https://maven1.aliyun.com/repository/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun-public2</id>
<url>https://maven.aliyun.com/repository/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 镜像配置
注意:镜像配置中 mirrorOf 不应当配置为*
,否则代理了所有,其他所有配置都没用了,都默认走这个url下载。即优先级最高,一般不这么干,除非就是想所有都走该镜像下载。
mirrorOf 标签里面放置的是 repository 配置的 id,为了满足一些复杂的需求,支持的高级配置如下:
external:*
,不在本地仓库的文件才从该镜像获取。repo,repo1
, 远程仓库 repo 和 repo1 从该镜像获取。*,!repo1
,所有远程仓库都从该镜像获取,除 repo1 远程仓库以外。*
,所用远程仓库都从该镜像获取。
示例:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf> // 注意这种情况太暴力,其他配置啥也废了。
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
2
3
4
5
6