簡介
通常我們需要同時使用比如阿里雲等國內鏡像庫,和公司內的私服庫。在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>
中的鏡像庫 -
有時候,會發現不管你怎麼修改配置,修改遠程庫地址,都無法生效,這個可以看一下我另一篇博文,希望有所幫助。