Mirror 与 Repositories 的区别

Mirror

mirror 相当于一个拦截器,它会拦截 Maven 对 remote repository 的相关请求,把请求里的 remote repository 地址重定向到 mirror 里配置的地址。

Repositories

步骤一:优先查询本地仓库地址是否存在,不存在,继续;

步骤二:从配置的 center repository 下载,没找到,继续;

步骤三:依次从配置的 <repositories> 下配置的一个或者多个远程仓库下载,如果均请求不到,就会报错了。

Maven 最佳配置

  1. 配置 mirror,避免中央镜像墙内网络问题
  2. 配置远程仓库地址,避免有些资源从单一仓库无法下载下来

方式一:全局配置

可以添加阿里云的镜像到 Mavensetting.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<mirrors>
<mirror>
<id>repo1</id>
<url>https://repo1.maven.org/maven2/</url>
<mirrorOf>${repo1}</mirrorOf>
</mirror>
<mirror>
<id>netease</id>
<url>http://mirrors.163.com/maven/repository/maven-public/</url>
<mirrorOf>${netease}</mirrorOf>
</mirror>
<mirror>
<id>personal</id>
<url>http://192.168.0.100/nexus/repository/maven-public/</url>
<mirrorOf>${personal}</mirrorOf>
</mirror>
<mirror>
<id>default</id>
<url>https://maven.aliyun.com/repository/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

注: <mirrorOf> 可以设置为哪个中央仓库做镜像,为名为 "central" 的中央仓库做镜像,写作 <mirrorOf>central</mirrorOf>;为所有中央仓库做镜像,写作 <mirrorOf>*</mirrorOf>。Maven 默认中央仓库的 id 为 central。id 是唯一的。

setting.xml 配置文件位置

添加镜像配置

配置仓库

setting.xml 不直接支持 repositoriespluginRepositories 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用 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 中,然后,使用 <activeProfiles> 元素自动激活该 profile。这样,你就不用再为每个 POM 重复配置仓库。使用 profile 为 settings.xml 添加仓库提供了一种用户全局范围的仓库配置。

方式二:单项目配置

单项目配置时,需要修改 pom 文件。pom 文件中,没有 mirror 元素。在 pom 文件中,通过覆盖默认的中央仓库的配置,实现中央仓库地址的变更。

注: 这里只是配置代理仓库,全局代理镜像记得要看 Maven 全局 setting 文件里的 mirror。

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>conifg</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

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

</project>