Mirror
mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
repositories
步骤一:优先查询本地仓库地址是否存在,不存在,继续;
步骤二:从配置的center repository 下载,没找到,继续;
步骤三,依次从配置的
下配置的一个或者多个远程仓库下载,如果均请求不到,就会报错了;
所以maven最佳配置:
- 配置mirror, 避免中央镜像墙内网络问题
- 配置远程仓库地址, 避免有些资源从单一仓库无法下载下来
方式一:全局配置
可以添加阿里云的镜像到maven的setting.xml配置中,这样就不需要每次在pom中,添加镜像仓库的配置,在mirrors节点下面添加子节点:
配置镜像
注:Maven默认中央仓库的id 为 central。id是唯一的。因此可以使用< id>central< /id>覆盖默认的中央仓库。
默认情况下配置多个mirror, 只有第一个生效
那么假如在公司使用内网, 但个人无法使用内网的情况下, 如何利用好多个mirror, 如下
如果希望使用maven.org 中央仓库,当前仓库下执行:mvn help-effective-settings -Drepo1=central
如果希望使用网易镜像,当前仓库下执行:mvn help-effective-settings -Dnetease=central
如果希望使用个人镜像,当前仓库下执行:mvn help-effective-settings -Dpersonal=central
否则默认使用default-阿里云
1 | <mirrors> |
1 | 注:< mirrorOf>可以设置为哪个中央仓库做镜像,为名为“central”的中央仓库做镜像,写作< mirrorOf>central< /mirrorOf>; |
setting.xml配置文件位置

添加镜像配置

配置仓库
setting.xml不直接支持 repositories,pluginRepositories 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用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 <settings>
...
<profiles>
<profile>
<id>dev</id>
<!-- repositories and pluginRepositories here-->
<repositories>
<repository>
<url>https://maven.aliyun.com/repository/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<url>https://maven.aliyun.com/repository/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
...
</settings>这里我们定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用
元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库。
使用profile为settings.xml添加仓库提供了一种用户全局范围的仓库配置。
方式二:单项目配置
单项目配置时,需要修改pom文件。pom文件中,没有mirror元素。在pom文件中,通过覆盖默认的中央仓库的配置,实现中央仓库地址的变更。
修改项目的pom文件:
注:这里只是配置代理仓库, 全局代理镜像记得要看maven全局setting文件里的mirror
1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |