配置Maven的settings.xml
在Maven中提供了一個settings.xml檔案來定義Maven的全域性環境資訊。這個檔案會存在於Maven的安裝目錄的conf子目錄下面,或者是使用者Home目錄的.m2子目錄下面。我們可以通過這個檔案來定義本地倉庫、遠端倉庫和聯網使用的代理資訊等。
安裝好maven 之後,可以參考${maven.home}/conf/settings.xml這個檔案,裡面有很多例子並有詳細解釋。
其實相對於多使用者的PC機而言,在Maven安裝目錄的conf子目錄下面的settings.xml才是真正的全域性的配置。而使用者家目錄的.m2子目錄下面的settings.xml的配置只是針對當前使用者的。當這兩個檔案同時存在的時候,那麼對於相同的配置資訊使用者家目錄下面的settings.xml中定義的會覆蓋Maven安裝目錄下面的settings.xml中的定義。使用者家目錄下的settings.xml檔案一般是不存在的,但是Maven允許我們在這裡定義我們自己的settings.xml,如果需要在這裡定義我們自己的settings.xml的時候就可以把Maven安裝目錄下面的settings.xml檔案拷貝到使用者家目錄的.m2目錄下,然後改成自己想要的樣子。
先來看一個基本的settings.xml的樣子
<?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"> <localRepository>D:\\develop\\mavenRepository</localRepository> <interactiveMode>true</interactiveMode> <offline>false</offline> <pluginGroups> <pluginGroup>com.your.plugins</pluginGroup> </pluginGroups> <proxies> <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> </proxies> <servers> <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> </servers> <mirrors> <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> </mirrors> <profiles> <profile> <id>jdk-1.5</id> <activation> <jdk>1.5</jdk> </activation> <repositories> <repository> <id>jdk15</id> <name>jdk1.5</name> <url>http://www.myhost.com/maven/jdk15</url> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>jdk-1.5</activeProfile> </activeProfiles> </settings>
localRepository:表示Maven用來在本地儲存資訊的本地倉庫的目錄。預設是使用者Home目錄下面的.m2/repository目錄。
interactiveMode:表示是否使用互動模式,預設是true;如果設為false,那麼當Maven需要使用者進行輸入的時候,它會使用一個預設值。
offline:表示是否離線,預設是false。這個屬性表示在Maven進行專案編譯和部署等操作時是否允許Maven進行聯網來下載所需要的資訊。
pluginGroups:在pluginGroups元素下面可以定義一系列的pluginGroup元素。pluginGroup元素指定的是plugin的groupId。
當一個plugin被使用,而它的groupId沒有被提供的時候,這個列表將被搜尋。
This is a list of additional group identifiers that will be searched when resolving plugins by their prefix。
當執行"mvn prefix:goal"這樣的命令的時候,會通過這裡指定的元素來通過字首來解析plugin。
預設情況下,Maven會自動把org.apache.maven.plugins和org.codehaus.mojo新增到pluginGroups下。
proxies:其下面可以定義一系列的proxy子元素,表示Maven在進行聯網時需要使用到的代理。當設定了多個代理的時候第一個標記active為true的代理將會被使用。
servers:其下面可以定義一系列的server子元素,表示當需要連線到一個遠端伺服器的時候需要使用到的驗證方式。這主要有username/password和privateKey/passphrase這兩種方式。
This is a list of authentication profiles, keyed by the server-id used within the system.
mirrors:用於定義一系列的遠端倉庫的映象。我們可以在pom中定義一個下載工件的時候所使用的遠端倉庫。但是有時候這個遠端倉庫會比較忙,所以這個時候人們就想著給它建立映象以緩解遠端倉庫的壓力,也就是說會把對遠端倉庫的請求轉換到對其映象地址的請求。每個遠端倉庫都會有一個id,這樣我們就可以建立自己的mirror來關聯到該倉庫,那麼以後需要從遠端倉庫下載工件的時候Maven就可以從我們定義好的mirror站點來下載,這可以很好的緩解我們遠端倉庫的壓力。
不過,很多internal repository搭建工具往往也提供mirror服務,比如Nexus就可以讓同一個URL,既用作internal repository,又使它成為所有repository的mirror。
- id:是用來區別mirror的,所有的mirror不能有相同的id
- mirrorOf:用來表示該mirror是關聯的哪一個倉庫,其值為其關聯倉庫的id。當要同時關聯多個倉庫時,這多個倉庫之間可以用逗號隔開;當要關聯所有的倉庫時,可以使用“*”表示;當要關聯除某一個倉庫以外的其他所有倉庫時,可以表示為“*,!repositoryId”;當要關聯不是localhost或用file請求的倉庫時,可以表示為“external:*”。
- url:表示該映象的url。當Maven在建立系統的時候就會使用這個url來連線到我們的遠端倉庫。
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查詢。
profiles:用於指定一系列的profile。
profile元素由activation、repositories、pluginRepositories和properties四個元素組成。當一個profile在settings.xml中是處於活動狀態並且在pom.xml中定義了一個相同id的profile時,settings.xml中的profile會覆蓋pom.xml中的profile。
(1)activation:這是profile中最重要的元素。跟pom.xml中的profile一樣,settings.xml中的profile也可以在特定環境下改變一些值,而這些環境是通過activation元素來指定的。
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.6</jdk>
<os>
<name>Windows 7</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>hello</name>
<value>world</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
在上面這段程式碼中,當所有的約束條件都滿足的時候就會啟用這個profile。a)jdk:表示當jdk的版本滿足條件的時候啟用,在這裡是1.6。這裡的版本還可以用一個範圍來表示,如
<jdk>[1.4,1.7)</jdk>表示1.4、1.5和1.6滿足;
<jdk>[1.4,1.7]</jdk>表示1.4、1.5、1.6和1.7滿足;
b)os:表示當作業系統滿足條件的時候啟用。
c)property:property是鍵值對的形式,表示當Maven檢測到了這樣一個鍵值對的時候就啟用該profile。如果只包含name元素,表示如果存在這個屬性,不管值多少,就啟用這個屬性。
這個時候如果要啟用該profile的話,可以在呼叫Maven指令的時候加上引數hello並指定其值為world,如:mvn compile –Dhello=world
d)file:表示當檔案存在或不存在的時候啟用,exists表示存在,missing表示不存在。
2)properties:用於定義屬性鍵值對的。當該profile是啟用狀態的時候,properties下面指定的屬性都可以在pom.xml中使用。
An encouraged best practice for profile identification is to use a consistent naming convention for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
This will make it more intuitive to understand what the set of introduced profiles is attempting to accomplish, particularly when you only have a list of profile id's for debug.
另外,profiles中的定義也同樣適用於pom.xml。例如我們在pom.xml中定義瞭如下的profile。
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pom.dubbo.protocol.port>-1</pom.dubbo.protocol.port>
<pom.dubbo.registry.protocol>multicast</pom.dubbo.registry.protocol>
<pom.dubbo.registry.address>224.0.0.1:12345</pom.dubbo.registry.address>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<pom.dubbo.protocol.port>-1</pom.dubbo.protocol.port>
<pom.dubbo.registry.protocol>zookeeper</pom.dubbo.registry.protocol>
<pom.dubbo.registry.address>10.1.1.4:2181</pom.dubbo.registry.address>
</properties>
</profile>
</profiles>
然後在spring 的配置檔案新增如下定義<dubbo:registry protocol="${pom.dubbo.registry.protocol}" address="${pom.dubbo.registry.address}" />
這樣預設是dev的profile被啟用,對應於開發版本。如果是生產版本,使用 mvn clean install -P prod 命令指定prod的profile。這樣就可以很方便的切換開發和生產環境。
3)repositories:用於定義遠端倉庫的,當該profile是啟用狀態的時候,這裡面定義的遠端倉庫將作為當前pom的遠端倉庫。
如果定義了多個倉庫,maven下載檔案的時候就會按照在配置檔案中出現的順序,在這些遠端倉庫中查詢,直到找到為止。
如果所有的遠端倉庫中都沒有找到,就會到中央倉庫中查詢。中央倉庫的定義如下:
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
maven中的倉庫分為兩種,snapshot快照倉庫和release釋出倉庫。snapshot快照倉庫用於儲存開發過程中的不穩定版本,release正式倉庫則是用來儲存穩定的發行版本。定義一個元件/模組為快照版本,只需要在pom檔案中在該模組的版本號後加上-SNAPSHOT即可(注意這裡必須是大寫)。
下面是一個settings.xml的例子。
<?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>
</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>
原文連結:
1)http://haohaoxuexi.iteye.com/blog/1827778
2)http://my.oschina.net/qjx1208/blog/201085