Maven仓库及镜像配置

摘要

  • maven仓库和镜像的配置及其关联方式

依赖查找顺序(按优先级排序,由高到低)

  • 本地仓库,setting.xml中settings.localRepository的配置地址

  • settings.xml中profile.repositories配置的仓库,settings.profiles.profile.repositories.repository,注意要激活对应的profile

  • pom.xml中profile.repositories配置的仓库,project.profiles.profile.repositories.repository,注意要在settings.xml中激活对应的profile

  • pom.xml中repositories配置的仓库,project.repositories.repository

  • maven默认的中央仓库:https://repo.maven.apache.org/maven2

说明,仓库的查找顺序实际上就是最终的pom.xml中仓库的声明顺序,所以可以通过如下命令导出最终的pom信息进行查看

1
mvn help:effective-pom -Doutput=EffectivePom.xml

仓库配置–repository

  • 可以将仓库信息配置在settings.xmlpom.xml中,如下可以配置多个,按顺序依次查找,注意这里的id必须唯一,repositorypluginRepository的id可以相同:

1
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
<repositories>
<repository>
<id>nexus</id>
<url>http://nexus.my.local:8080/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://nexus.my.local:8080/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>

<pluginRepository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

镜像代理–mirror

说明

  • 由于网络等原因导致我们不能很顺利的从仓库下载到对应的依赖,此时我们可以为仓库配置镜像代理,加快下载速度,国内一般会使用阿里云作为镜像代理

  • mirror镜像可以配置多个,每个mirrorOf里可以配置如下内容:

  1. * : 匹配所有仓库,一般我们搭建了自己的私服时推荐这样设置

  2. repo1Id : 匹配仓库id为repo1Id的仓库

  3. repo1Id,repo2Id : 匹配仓库id为repo1Id和repo2Id的仓库

  4. *,!repo1Id : 匹配所有仓库,但不包含repo1Id

  5. external:* : 匹配所有不在本机上的远程仓库,也就是说,如果仓库的url里配置的是localhost或者file://则不进行代理

镜像代理需要配置在setting.xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<mirrors>
<mirror>
<id>aliyun-mirror</id>
<name>aliyun-mirror</name>
<!-- mirrorOf 里配置要代理的仓库id,这样当请求该仓库时就会走这个镜像代理 -->
<!-- maven默认配置的仓库id就是central,此时就是使用阿里云作为默认仓库的代理 -->
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>

<mirror>
<id>nexus-mirror</id>
<name>nexus-mirror</name>
<!-- mirrorOf 里配置要代理的仓库id,这样当请求该仓库时就会走这个镜像代理 -->
<!-- 此时就是使用nexus私服代理所有仓库 -->
<mirrorOf>*</mirrorOf>
<url>http://nexus.my.local:8080/repository/maven-public/</url>
</mirror>
</mirrors>

maven3.8.1以后的配置中会禁止http协议的仓库地址,该如何处理?

  • 我们可以看到setting.xml中的镜像配置里默认增加了如下配置,其会禁止所有http协议的仓库被请求,目的是推荐使用https协议

1
2
3
4
5
6
7
<mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
  • 此时执行mvn时会报告如下错误:

1
maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories
  • 但是很多公司的私服都没有配置证书,我们可以修改其配置如下,即可解决

1
2
3
4
5
6
7
<mirror>
<id>nexus</id>
<!-- <mirrorOf>external:*</mirrorOf> -->
<mirrorOf>external:http:*</mirrorOf>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<blocked>false</blocked>
</mirror>

仓库配置–profile分环境配置多个仓库

  • 一般我们在公司都会连接公司的私服,但是私服一般不支持外网访问,如果不能vpn到公司的环境,我们就需要切换镜像的地址,此时可以通过配置profile的方式进行解决,我们可以配置多个profile,使用时激活对应的profile即可

  • 配置profile允许maven根据不同的环境采用不同的maven配置,一个profiles标签中可以有很多个profile,只需要根据不同的项目环境,激活不同的profile即可。

1
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
55
56
57
58
59
60
61
62
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>

<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>

</profiles>

<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
  • 默认激活的就是nexus对应的私服,激活其它profile可以修改配置文件的activeProfile,或者使用如下命令

1
mvn clean package -P aliyun