一. 本地倉庫
本地倉庫是遠程倉庫的一個緩沖和子集,當你構建Maven項目的時候,首先會從本地倉庫查找資源,如果沒有,那么Maven會從遠程倉庫下載到你本地倉庫。這樣在你下次使用的時候就不需要從遠程下載了。如果你所需要的jar包版本在本地倉庫沒有,而且也不存在於遠程倉庫,Maven在構建的時候會報錯,這種情況可能發生在有些jar包的新版本沒有在Maven倉庫中及時更新。
Maven缺省的本地倉庫地址為${user.home}/.m2/repository 。也就是說,一個用戶會對應的擁有一個本地倉庫。當然你可以通過修改${user.home}/.m2/settings.xml 配置這個地址:
Xml代碼
-
<settings>
-
<localRepository> E:/repository/maven/repos</localRepository>
-
</settings>
如果你想讓所有的用戶使用統一的配置那么你可以修改Maven主目錄下的setting.xml:
${M2_HOME}/conf/setting.xml
二. 遠程倉庫
除本地倉庫以外的倉庫都叫做遠程倉庫
本地倉庫配置在: <localRepository> E:/repository/maven/repos</localRepository>
遠程倉庫配置在:
<profiles>
<profile>
<id></id>
<repositories>
<repository>遠程倉庫配置</repository>
</repositories>
</profile>
</profiles>
中央倉庫也屬於遠程倉庫的一種,特征就是 <id>central</id> id名為 central,
Maven的中央倉庫地址默認是:https://repo.maven.apache.org/maven2/,可以通過修改settings.xml文件來修改默認的中央倉庫地址:
<profile> <id>repository-profile</id> <repositories> <repository> <id>central</id> <name>Central Repository</name> <layout>default</layout> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> </profile>
要注意的是如果修改的是中央倉庫地址,那么repository下面的id標簽值一定得是central,此外,還需要激活這個profile才能生效,這里的標簽值就是profile標簽下面的id標簽值
<activeProfiles> <activeProfile>repository-profile</activeProfile> </activeProfiles>
四. Nexus私服
私服也屬於遠程倉庫的一種,只是這個遠程倉庫的地址是本地服務器而已

<project>
...
<!-- 配置私服地址 -->
<repositories>
<repository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</pluginRepository>
</pluginRepositories>
...
<project>
<settings>
...
<profiles>
<profile>
<id>profile-nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
...
</settings>
Maven的鏡像是指如果倉庫X可以提供倉庫Y存儲的所有內容,那么就可以認為X是Y的一個鏡像,也就是說任何一個可以從倉庫Y獲得的依賴,都能夠從它的鏡像中獲取。比如阿里的Maven倉庫http://maven.aliyun.com/nexus/content/groups/public就可以理解為是中央倉庫https://repo.maven.apache.org/maven2/在中國的鏡像,由於地理位置的因素,該鏡像往往能夠提供比中央倉庫更快的服務。要為一個倉庫配置鏡像只需要修改settings.xml文件,如下,其中的mirrorOf標簽值就是倉庫的id標簽值,中央倉庫就是默認就是central,表示這是為中央倉庫配置的鏡像,以后所有的依賴下載都會從這個鏡像中進行下載。
<mirror> <id>central-repository-mirror</id> <name>Central Repository Mirror</name> <mirrorOf>central</mirrorOf> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
repository是指在局域網內部搭建的repository,它跟central repository, jboss repository等的區別僅僅在於其URL是一個內部網址
mirror則相當於一個代理,它會攔截去指定的遠程repository下載構件的請求,然后從自己這里找出構件回送給客戶端。配置mirror的目的一般是出於網速考慮。
不過,很多internal repository搭建工具往往也提供mirror服務,比如Nexus就可以讓同一個URL,既用作internal repository,又使它成為所有repository的mirror。
高級的鏡像配置:1.<mirrorOf>*</mirrorOf>
匹配所有遠程倉庫。這樣所有pom中定義的倉庫都不生效
2.<mirrorOf>external:*</mirrorOf>
匹配所有遠程倉庫,使用localhost的除外,使用file://協議的除外。也就是說,匹配所有不在本機上的遠程倉庫。
3.<mirrorOf>repo1,repo2</mirrorOf>
匹配倉庫repo1和repo2,使用逗號分隔多個遠程倉庫。
4.<mirrorOf>*,!repo1</miiroOf>
匹配所有遠程倉庫,repo1除外,使用感嘆號將倉庫從匹配中排除。
mirrors可以配置多個mirror,每個mirror有id,name,url,mirrorOf屬性,id是唯一標識一個mirror就不多說了,name貌似沒多大用,相當於描述,url是官方的庫地址,mirrorOf代表了一個鏡像的替代位置,例如central就表示代替官方的中央庫。
我本以為鏡像庫是一個分庫的概念,就是說當a.jar在第一個mirror中不存在的時候,maven會去第二個mirror中查詢下載。但事實卻不是這樣,當第一個mirror中不存在a.jar的時候,並不會去第二個mirror中查找,甚至於,maven根本不會去其他的mirror地址查詢。
后來終於知道,maven的mirror是鏡像,而不是“分庫”,只有當前一個mirror無法連接的時候,才會去找后一個,類似於備份和容災。
還有,mirror也不是按settings.xml中寫的那樣的順序來查詢的。
所謂的第一個並不一定是最上面的那個。
當有id為B,A,C的順序的mirror在mirrors節點中,maven會根據字母排序來指定第一個,所以不管怎么排列,一定會找到A這個mirror來進行查找,當A無法連接,出現意外的情況下,才會去B查詢。
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>repo-iss</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
<mirrors>
<!-- osc鏡像 -->
<mirror>
<!-- 鏡像所有遠程倉庫,但不包括指定的倉庫 -->
<id>mirror-osc</id>
<mirrorOf>external:*,!repo-osc-thirdparty,!repo-iss</mirrorOf>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
<!--
<mirror>
<id>mirror-iss</id>
<mirrorOf>external:*</mirrorOf>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
</mirror>
-->
</mirrors>
<profiles>
<profile>
<id>profile-default</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>repo-osc-thirdparty</id>
<url>http://maven.oschina.net/content/repositories/thirdparty/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>profile-iss</id>
<repositories>
<repository>
<id>repo-iss</id>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo-iss</id>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>profile-default</activeProfile>
<!--<activeProfile>profile-iss</activeProfile>-->
</activeProfiles>
<!--
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>10.10.204.160</host>
<port>80</port>
</proxy>
</proxies>
-->
</settings>
Reference:
https://my.oschina.net/zhanghaiyang/blog/606130