1. 程式人生 > >Maven的配置檔案settings.xml

Maven的配置檔案settings.xml

轉自:http://www.cnblogs.com/yakov/archive/2011/11/26/maven2_settings.html

http://www.yiibai.com/maven/maven_environment_setup.html

簡介:

概覽

當Maven執行過程中的各種配置,例如pom.xml,不想繫結到一個固定的project或者要分配給使用者時,我們使用settings.xml中的settings元素來確定這些配置。這包含了本地倉庫位置,遠端倉庫伺服器以及認證資訊等。

settings.xml存在於兩個地方:

1.安裝的地方:$M2_HOME/conf/settings.xml

2.使用者的目錄:${user.home}/.m2/settings.xml

前者又被叫做全域性配置,後者被稱為使用者配置。如果兩者都存在,它們的內容將被合併,並且使用者範圍的settings.xml優先。

如果你偶爾需要建立使用者範圍的settings,你可以簡單的copy Maven安裝路徑下的settings到目錄${user.home}/.m2。Maven預設的settings.xml是一個包含了註釋和例子的模板,你可以快速的修改它來達到你的要求。

下面是settings下的頂層元素的一個概覽:

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 <localRepository/>
6 <interactiveMode/>
7 <usePluginRegistry/>
8 <offline/>
9 <
pluginGroups/>
10 <servers/>
11 <mirrors/>
12 <proxies/>
13 <profiles/>
14 <activeProfiles/>
15 </settings>
複製程式碼

settings的內容可以在下面這些地方篡改:

1.${user.home}和所有其他的系統屬性

2.${env.HOME}等環境變數

注意:settins.xml中profiles下定義的屬性不能被篡改。

配置細節:

簡單的值

一半以上的頂級settings元素師簡單的值,代表了一直處於活躍的構建系統的元素的取值範圍。

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 2           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 <localRepository>${user.home}/.m2/repository</localRepository>
6 <interactiveMode>true</interactiveMode>
7 <usePluginRegistry>false</usePluginRegistry>
8 <offline>false</offline>
9 ...
10 </settings>
複製程式碼

localRepository:這個值是構建系統的本地倉庫的路徑。預設的值是${user.home}/.m2/repository.如果一個系統想讓所有登陸的使用者都用同一個本地倉庫的話,這個值是極其有用的。

interactiveMode:如果Maven要試圖與使用者互動來得到輸入就設定為true,否則就設定為false,預設為true。

usePluginRegistry:如果Maven使用${user.home}/.m2/plugin-registry.xml來管理plugin的版本,就設定為true,預設為false。

offline:如果構建系統要在離線模式下工作,設定為true,預設為false。如果構建伺服器因為網路故障或者安全問題不能與遠端倉庫相連,那麼這個設定是非常有用的。

外掛組

這個元素包含了一系列pluginGroup元素,每個又包含了一個groupId。當一個plugin被使用,而它的groupId沒有被提供的時候,這個列表將被搜尋。這個列表自動的包含了org.apache.maven.plugins和org.codehaus.mojo。

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 2           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 ...
6 <pluginGroups>
7 <pluginGroup>org.mortbay.jetty</pluginGroup>
8 </pluginGroups>
9 ...
10 </settings>
複製程式碼

例如,有了上面的配置,Maven命令列可以使用簡單的命令執行org.morbay.jetty:jetty-maven-plugin:run,如下

mvn jetty run

伺服器

用來下載和部署的倉庫是用POM中的repositories和distributionManagement元素來定義的。但是某些配置例如username和password就不應該隨著pom.xml來分配了。這種型別的資訊應該儲存在構建伺服器中的settings.xml中。

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 2           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 ...
6 <servers>
7 <server>
8 <id>server001</id>
9 <username>my_login</username>
10 <password>my_password</password>
11 <privateKey>${user.home}/.ssh/id_dsa</privateKey>
12 <passphrase>some_passphrase</passphrase>
13 <filePermissions>664</filePermissions>
14 <directoryPermissions>775</directoryPermissions>
15 <configuration></configuration>
16 </server>
17 </servers>
18 ...
19 </settings>
複製程式碼

id:這是Server的ID(不是登入進來的user),與Maven想要連線上的repository/mirror中的id元素相匹配。

username,password:這兩個元素成對出現,表示連線這個server需要驗證username和password。

privateKey,passphrase:與前兩個元素一樣,這兩個成對出現,分別指向了一個私鑰(預設的是${user.home}/.ssh/id_dsa)和一個passphrase。passphrase和password元素可能在將來被客觀化,但是現在必須以文字形式在settings.xml中設定。

filePermissions,directoryPermissions:當一個倉庫檔案或者目錄在部署階段被建立的時候,就必須用到許可權許可。他們合法的值是三個數字,就像*nix中的檔案許可權,例如:664,775.

注意:如果你使用了一個私鑰來登入server,那麼password元素必須被省略,否則私鑰將被忽視。

密碼加密

一個新特徵:伺服器password和passphrase加密已經被升到2.1.0+

映象

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 2           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 ...
6 <mirrors>
7 <mirror>
8 <id>planetmirror.com</id>
9 <name>PlanetMirror Australia</name>
10 <url>http://downloads.planetmirror.com/pub/maven2</url>
11 <mirrorOf>central</mirrorOf>
12 </mirror>
13 </mirrors>
14 ...
15 </settings>
複製程式碼

id,name:唯一的映象標識和使用者友好的映象名稱。id被用來區分mirror元素,並且當連線時候被用來獲得相應的證書。

url:映象基本的URL,構建系統敬將使用這個URL來連線倉庫,而不是原來的倉庫URL。

mirrorOf:映象所包含的倉庫的Id。例如,指向Maven central倉庫的映象(http://repo1.maven.org/maven2/),設定這個元素為central。更多的高階對映例如repo1,repo2 或者*,!inhouse都是可以的。沒必要一定和mirror的id相匹配。

代理

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 2           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 ...
6 <proxies>
7 <proxy>
8 <id>myproxy</id>
9 <active>true</active>
10 <protocol>http</protocol>
11 <host>proxy.somewhere.com</host>
12 <port>8080</port>
13 <username>proxyuser</username>
14 <password>somepassword</password>
15 <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
16 </proxy>
17 </proxies>
18 ...
19 </settings>
複製程式碼

id:proxy的唯一標識,用來區別proxy元素。

active:當proxy被啟用的時候為true。當申明的代理很多的時候,這個很有用,但是同一時間僅有一個被啟用。

protocol,host,port:代理地址protocol://host:port的分散形式。

username,password:兩個元素成對出現,提供連線proxy伺服器時的認證。

nonProxyHosts:這裡列出了不需要使用代理的hosts。列表的分隔符是proxy伺服器想要的型別。上面例子使用了pipe分隔符,逗號分隔符也比較通用。

配置檔案

settings.xml中的profile是pom.xml中的profile的簡潔形式。它包含了啟用(activation),倉庫(repositories),外掛倉庫(pluginRepositories)和屬性(properties)元素。profile元素僅包含這四個元素是因為他們涉及到整個的構建系統,而不是個別的POM配置。

如果settings中的profile被啟用,那麼它的值將過載POM或者profiles.xml中的任何相等ID的profiles。

啟用(activation)

activations是profile的關鍵,就像POM中的profiles,profile的能力在於它在特定情況下可以修改一些值。而這些情況是通過activation來指定的。

複製程式碼
 1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 2           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4 http://maven.apache.org/xsd/settings-1.0.0.xsd">
5 ...
6 <profiles>
7 <profile>
8 <id>test</id>
9 <activation>
10 <activeByDefault>false</activeByDefault>
11 <jdk>1.5</jdk>
12 <os>
13 <name>Windows XP</name>
14 <family>Windows</family>
15 <arch>x86</arch>
16 <version>5.1.2600</version>
17 </os>
18 <property>
19 <name>mavenVersion</name>
20 <value>2.0.3</value>
21 </property>
22 <file>
23 <exists>${basedir}/file2.properties</exists>
24 <missing>${basedir}/file1.properties</missing>
25 </file>
26 </activation>
27 ...
28 </profile>
29 </profiles>
30 ...
31 </settings>
複製程式碼

如果所有指定的條件都達到了,那麼,activation就被觸發,而且不需要一次性全部達到。

jdk:在jdk元素中,activation有一個內建的,java版本檢測。如果檢測到jdk版本與期待的一樣,那麼就啟用。在上面的例子中,1.5.0_06是滿足的。

os:os元素可以定義一些上面所示的作業系統特定的屬性。

property:如果Maven檢測到相應的名值對的屬性,那麼,這個profile將被啟用。

file:如果給定的檔案存在,或者不存在那麼將啟用這個profile。

activation並不是唯一啟用profile的途徑。settings.xml中的activeProfile包含了profile的id