一、mirror

1、<mirrors>的描述

This is a list of mirrors to be used in downloading artifacts from remote repositories. It works like this: a POM may declare a repository to use in resolving certain artifacts.However, this repository may have problems with heavy traffic at times, so people have mirrored it to several places.

That repository definition will have a unique id, so we can create a mirror reference for that repository, to be used as an alternate download site. The mirror site will be the preferred server for that repository.

2、<mirrors> 中的 <mirror> 描述

Specifies a repository mirror site to use instead of a given repository. The repository that this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used for inheritance and direct lookup purposes, and must be unique across the set of mirrors.

3、镜像的默认配置

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>

4、<mirror> 中的 <mirrorOf>

1
<mirrorOf>*</mirrorOf>

配置规则如下:

  • <mirrorOf>*</mirrorOf>,匹配所有远程仓库
  • <mirrorOf>repo1,repo2</mirrorOf>,匹配多个远程仓库,用逗号分隔
  • <mirrorOf>*,!repo1</mirrorOf>,匹配所有远程仓库,repo1 除外

我的配置:

1
2
3
4
5
6
<mirror>
<id>aliyun-maven</id>
<mirrorOf>central,central-aliyun,central-maven,aliyun-spring</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>

二、repository

1、settings 中的配置

settings 文件中的 repository 需要在 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
<profiles>
<profile>
<id>default-maven-repositories</id>
<repositories>
<repository>
<id>central-aliyun</id>
<url>https://maven.aliyun.com/repository/central</url>
<name>aliyun central repository</name>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<repository>
<id>aliyun-spring</id>
<url>https://maven.aliyun.com/repository/spring</url>
<name>aliyun spring repository</name>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<repository>
<id>central</id>
<name>Apache Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>

<repository>
<id>central-maven</id>
<name>maven Central Repository</name>
<url>https://repo1.maven.org/maven2/</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>

不自定义远程仓库的话,maven 的默认仓库为https://repo.maven.apache.org/maven2

上面的配置默认是不会激活的,激活属性文件的两种方式:

1
2
3
4
5
<settings>
<activeProfiles>
<activeProfile>default-maven-repositories</activeProfile>
</activeProfiles>
</settings>
1
2
3
4
5
6
7
8
9
10
<settings>
<profiles>
<profile>
<id>default-maven-repositories</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<settings>

上面修改的 settings 文件是用户文件夹下的 .m2 目录下的 settings 文件,该配置文件对当前系统登录用户生效,如果配置了上述的属性默认激活配置,则用户所建的所有项目都会默认激活该配置。

2、POM 中的配置

上面 settings 中的 repository 是默认激活的属性,项目的 POM 中不用再进行额外重复的配置了,除非需要用到一些中央仓库中没有的依赖,比如下面的 activiti 的依赖,在中央仓库中没有较新版本的依赖,需要自定义 activiti 官网提供的远程仓库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<project>
<repositories>
<repository>
<id>activiti-releases</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases</url>
</repository>

<repository>
<id>activiti-snapshots</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

相关链接

maven镜像_maven下载地址_maven安装教程-阿里巴巴开源镜像站 (aliyun.com)

Maven – Welcome to Apache Maven

Maven Repository: Search/Browse/Explore (mvnrepository.com)

OB tags

#Maven