03-2.Maven專案怎麼呼叫遠端倉庫,及映象配置mirrors的使用
Maven 有個超級POM,它預設的位置在:maven-model-builder-3.3.9.jar\org\apache\maven\model\pom-4.0.0.xml,裡面配置了maven預設呼叫遠端倉庫和外掛倉庫的地址,如下:
<repositories>
<repository><!--依賴中央倉庫-->
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url >
<layout>default</layout><!--倉庫佈局:預設-->
<snapshots>
<enabled>false</enabled><!--下載快照版本:關閉-->
</snapshots>
</repository>
</repositories>
<pluginRepositories><!--外掛的中央倉庫-->
<pluginRepository >
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases >
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
下面在講講Mirror
mirror相當於一個攔截器,它會攔截maven對remote repository的相關請求,把請求裡的remote repository地址,重定向到mirror裡配置的地址。
mirrorOf標籤裡面放置的是要映象的遠端庫,它當於一個代理,會攔截去指定的遠端repository下載構件的請求。
比如上面兩個圖,正常呼叫遠端倉庫時候,maven回去找ID為A的對應的遠端倉庫,設定mirror後,mirrorOf為A,則會攔截請求為A的倉庫呼叫,然後重定向到B
高階的映象配置:
1.*
匹配所有遠端倉庫。 這樣所有pom中定義的倉庫都不生效
即,映象的mirrorof如果設定成*可以攔截所有的url請求,並把他重定向到映象設定的url路徑上
2.external:*
匹配所有遠端倉庫,使用localhost的除外,使用file://協議的除外。也就是說,匹配所有不在本機上的遠端倉庫。
3.repo1,repo2
匹配倉庫repo1和repo2,使用逗號分隔多個遠端倉庫。
4.*,!repo1
匹配所有遠端倉庫,repo1除外,使用感嘆號將倉庫從匹配中排除。
正如上面所講到的如果maven有自己預設配置好的呼叫遠端倉庫和遠端外掛倉庫的地址,如果我們在mirrors裡做如下配置,則會替代原來預設的遠端倉庫呼叫地址
<mirror>
<id>alimaven</id><!--此時的映象 -->
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
原來預設配置的倉庫ID是central,所以這裡的mirrorof為central,這樣就會攔截預設的倉庫呼叫,然後轉到對應我們配置好的阿里雲的倉庫地址上
我們知道pom.xml裡面的 repository和pluginRepository標籤可以設定遠端的倉庫地址,同時settings.xml裡的profile標籤裡的repository和pluginRepository標籤可以設定遠端的倉庫地址,同時設定時,pom裡的會覆蓋settings裡的。
一般來說,配置了私服的maven呼叫遠端倉庫順序試著樣的
1)如果沒有把預設的central倉庫配置到映象裡
當要下載依賴時,第一步:找到setting.xml中啟用的profile下repository裡id為xxx的配置,而xxx已經配置在裡映象裡,如下
程式碼中repository的id為nexus,查詢mirrors列表發現有一個的mirrorof裡是neuxs,則會使用mirror配置的url去查詢。那麼就會直接用repository的url去獲取。獲取規則詳見:http://blog.csdn.net/isea533/article/details/22437511
<profile>
<id>Nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.8.244:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://192.168.8.244:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<mirror>
<id>tets-cent</id><!--此時的映象 -->
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>nexus</mirrorOf>
</mirror>
這時會去repository中id對應的映象裡的url(倉庫)http://192.168.8.244:8081/nexus/content/groups/public裡下載依賴。當發現映象裡配置的url(倉庫)裡下載不到對應的依賴時,會自動去找到maven中預設的id為central,url為中央倉庫地址的repository配置,因為central沒有配置在映象中,所以此時可以去到maven中央倉庫下載依賴包
2)如果已經把預設的central倉庫配置到映象裡
(1)配置了映象後,當要下載依賴時,第一步:找到setting.xml中啟用的profile下repository裡id為xxx的配置,而xxx已經配置在裡映象裡
(2)這個時候會去到到映象裡的url(倉庫)裡下載依賴
(3)當發現映象裡配置的url(倉庫)裡下載不到對應的依賴時,會自動去找到maven中預設的id為central,url為中央倉庫地址的repository配置,
(4)此時central配置在映象中,所以這次是去到到映象裡的url(倉庫)裡下載依賴了。而不會去訪問maven中央倉庫了。