文章目录
- 简介
- 全局配置
- 单独配置
- 注意事项
简介
通常我们需要同时使用比如阿里云等国内镜像库,和公司内的私服库。在maven中,有2种方式可以配置多个远程库,来同时满足加速公共库,同时又能访问私有库的方式。
全局配置
全局配置,主要是在settings配置文件中配置,可以将常用的公共库,私服库配置进去。这样就不用在每个项目的pom文件中去配置了。
首先在settings配置文件中的<profiles></profiles>
节点中定义好远程库,比如下面定义了阿里云的公共库和Cloudera的公共库,可以根据需要将公司私服库也定义进去:
<profiles>
<profile>
<id>cloudera-repository</id>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>aliyun-repository</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
然后,必须要激活
<activeProfiles>
<activeProfile>aliyun-repository</activeProfile>
<activeProfile>cloudera-repository</activeProfile>
</activeProfiles>
特别要注意对应关系
activeProfile 对应的是<profile>
中的id,不是<profile>
中repository的id
单独配置
这个主要是在每个项目中的pom文件中配置。比如只有此项目要使用的某个私服库时可以配置进去。当然常用的库也可以选择配在这里而不是配置settings中,看个人需求。比如这样:
<repositories>
<repository>
<id>aliyun-r</id>
<name>aliyun maven repo remote</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
注意事项
- 我们都知道中央仓库速度比较慢,通常会去配置国内镜像库来加速访问。但是不是很推荐在
<mirrors>
中去配置国内镜像库的镜像。因为这是镜像,不是分库,就是说maven并不会帮忙做这样事:a.jar在第一个mirror中不存在,拉不下来,使用第二个镜像来尝试拉取。因此,在<mirrors>
定义多个镜像,幻想当第一个镜像中没有时去第二个镜像下载是行不通的,maven只会在第一个镜像连接失败的时候,才会尝试连接第二个镜像。(甚至有人说这个顺序都不是按照定义mirror的顺序,而是通过mirror id的字母排序?这个我没有深究)。其实要达到配置国内镜像库来加速访问的目的,可以就像上面全局配置那样,将国内镜像库比如阿里云,配置为远程库中的一个并激活即可,这样就可以加速了,或者单个项目中pom中配置,没有必要配置为<mirrors>
中的镜像库 - 有时候,会发现不管你怎么修改配置,修改远程库地址,都无法生效,这个可以看一下我另一篇博文,希望有所帮助。