這兩天在連公司的兩個不同的私服,但是總是出現JAR包不能全部下載下來的問題。經過查資料,大概有了點理解,記錄下
1、Jar包分布
公司的快照JAR全放在http://*:8081/*/libs-snapshot上
其他第三方包全放在http://*:8081/*/libs-public-local上
2、問題描述
不能同時從兩個地方下載到所有的JAR包
3、問題解決
經過不斷的摸索和查詢信息,最終能同時從兩個地方拉取JAR包,配置如下:
<mirrors> <mirror> <id>snapshots</id> <mirrorOf>snapshot</mirrorOf> <name>snapshots</name> <url>http://*:8081/*/libs-snapshot</url> </mirror> <mirror> <id>public</id> <mirrorOf>*,!snapshot</mirrorOf> <name>public</name> <url>http://*:8081/*/libs-public-local</url> </mirror> </mirrors>
<profiles> <profile> <id>xyz</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>libs-public</id> <name>libs-public</name> <url>http://*:8081/*/libs-public-local</url> </repository> <repository> <id>snapshot</id> <url>http://*:8081/*/libs-snapshot</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>ali</id> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
<activeProfiles> <activeProfile>xyz</activeProfile> </activeProfiles>
4、個人理解
在配置多個遠程倉庫的時候,需要借助repository和mirror來配置,倉庫指定了什么樣的包走這個倉庫ID進行下載,而mirror則對倉庫進行了地址代理,對應ID的包都走這個鏡像。
其中 mirrorof配置非常關鍵,指定了倉庫ID,其配置有多種,可以看官網,簡單摘抄點
* = everything 所有的倉庫都走這個鏡像
external:* = everything not on the localhost and not file based. 不在本地,且不一file: 開頭的東東,反正是外部的
repo,repo1 = repo or repo1 或
*,!repo1 = everything except repo1 出repo1倉庫之外的其他倉庫
另外,網上還有種說法是:配置了mirror,repository就不起作用了,我覺得可能說法有問題,當且僅當mirrorof配置成 * 時,才是那樣的結果,其實也不是不起作用了,只不過mirror把所有repository都給“代理”了。另一種說法是多個mirror時,只有第一次才生效,其實也不完全正確,只有當所有的mirrorof相同的時候,說法才正確。官網的說法是
Note that there can be at most one mirror for a given repository. In other words, you cannot map a single repository to a group of mirrors that all define the same <mirrorOf> value. Maven will not aggregate the mirrors but simply picks the first match.
還有,當mirrorof不能精確匹配時,maven會選取第一個鏡像
When Maven looks for a mirror of some repository, it first checks for a mirror whose <mirrorOf> exactly matches the repository identifier. If no direct match is found, Maven picks the first mirror declaration that matches according to the rules above (if any). Hence, you may influence match order by changing the order of the definitions in the settings.xml
5、遺留問題
在setting中配置多個倉庫后,maven怎么分配哪個包從哪個倉庫下呢,我大概知道點是配置repository的 Activation。但是不知道的是如果不配置Activation(如下例),那怎么區分呢,希望知道的小伙伴說下,多謝了
什么時候走 public? 什么時候走ali <repository> <id>libs-public</id> <name>libs-public</name> <url>http://*:8081/*/libs-public-local</url> </repository> <repository> <id>ali</id> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </repository>
備注下:會依次嘗試下載