在Maven安裝目錄下有一個settings.xml文件(
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:\Maven\repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
</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>
</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主要的jar保存位置,默認在${user.dir}/.m2/repository,如果需要另外設置,就換成其他的路徑。
interactiveMode:如果Maven需要和用戶交互以獲得輸入,則設置成true,反之則應爲false。默認爲true。
offline:如果不想每次編譯,都去查找遠程中心庫,那就設置爲true。當然前提是你已經下載了必須的依賴包。
usePluginRegistry:如果需要讓Maven使用文件plugin-registry.xml來管理插件版本,則設爲true。默認爲false。
profiles: 項目構建的配置信息,這裏會有單獨說明。
activeProfiles:激活的profile列表,按順序生效。
pluginGroups: 如果插件groupId未指明,按該列表下的id去查找。
proxies: 其下面可以定義一系列的proxy子元素,表示Maven在進行聯網時需要使用到的代理。當設置了多個代理的時候第一個標記active爲true的代理將會被使用。下面是一個使用代理的例子:
<proxies>
<proxy>
<id>xxx</id>
<active>true</active>
<protocol>http</protocol>
<username>用戶名</username>
<password>密碼</password>
<host>代理服務器地址</host>
<port>代理服務器的端口</port>
<nonProxyHosts>不使用代理的主機</nonProxyHosts>
</proxy>
</proxies>
servers:其下面可以定義一系列的server子元素,表示當需要連接到一個遠程服務器的時候需要使用到的驗證方式。這主要有username/password和privateKey/passphrase這兩種方式。以下是一個使用servers的示例:
<servers>
<server>
<id>id</id>
<username>用戶名</username>
<password>密碼</password>
</server>
</servers>
mirrors:用於定義一系列的遠程倉庫的鏡像。我們可以在pom中定義一個下載工件的時候所使用的遠程倉庫。但是有時候這個遠程倉庫會比較忙,所以這個時候人們就想着給它創建鏡像以緩解遠程倉庫的壓力,也就是說會把對遠程倉庫的請求轉換到對其鏡像地址的請求。每個遠程倉庫都會有一個id,這樣我們就可以創建自己的mirror來關聯到該倉庫,那麼以後需要從遠程倉庫下載工件的時候Maven就可以從我們定義好的mirror站點來下載,這可以很好的緩解我們遠程倉庫的壓力。在我們定義的mirror中每個遠程倉庫都只能有一個mirror與它關聯,也就是說你不能同時配置多個mirror的mirrorOf指向同一個repositoryId。
看以下是一個使用mirrors的例子:
<mirrors>
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>定義一個容易看懂的名稱 </name>
<url>http://my.repository.com/repo/path</url>
</mirror>
</mirrors>
- id:是用來區別mirror的,所有的mirror不能有相同的id
- mirrorOf:用來表示該mirror是關聯的哪一個倉庫,其值爲其關聯倉庫的id。當要同時關聯多個倉庫時,這多個倉庫之間可以用逗號隔開;當要關聯所有的倉庫時,可以使用“”表示;當要關聯除某一個倉庫以外的其他所有倉庫時,可以表示爲“,!repositoryId”;當要關聯不是localhost或用file請求的倉庫時,可以表示爲“external:*”。
- url:表示該鏡像的url。當Maven在建立系統的時候就會使用這個url來連接到我們的遠程倉庫。
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>mavenVersion</name>
<value>2.0.3</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
在上面這段代碼中,當所有的約束條件都滿足的時候就會激活這個profile。
- jdk:表示當jdk的版本滿足條件的時候激活,在這裏是1.6。這裏的版本還可以用一個範圍來表示,如
[1.4,1.7)表示1.4、1.5和1.6滿足;
[1.4,1.7]表示1.4、1.5、1.6和1.7滿足; - os:表示當操作系統滿足條件的時候激活。
- property:property是鍵值對的形式,表示當Maven檢測到了這樣一個鍵值對的時候就激活該profile。
1.1 下面的示例表示當存在屬性hello的時候激活該profile。
<property>
<name>hello</name>
</property>
1.2 下面的示例表示當屬性hello的值爲world的時候激活該profile。
<property>
<name>hello</name>
<value>world</value>
</property>
這個時候如果要激活該profile的話,可以在調用Maven指令的時候加上參數hello並指定其值爲world,如:
mvn compile –Dhello=world
- file:表示當文件存在或不存在的時候激活,exists表示存在,missing表示不存在。如下面的例子表示當文件hello/world不存在的時候激活該profile。
<profile>
<activation>
<file>
<missing>hello/world</missing>
</file>
</activation>
</profile>
- activeByDefault:當其值爲true的時候表示如果沒有其他的profile處於激活狀態的時候,該profile將自動被激活。
1.2properties:用於定義屬性鍵值對的。當該profile是激活狀態的時候,properties下面指定的屬性都可以在pom.xml中使用。
1.3 repositories:用於定義遠程倉庫的,當該profile是激活狀態的時候,這裏面定義的遠程倉庫將作爲當前pom的遠程倉庫。
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
- releases、snapshots:這是對於工件的類型的限制。
- enabled:表示這個倉庫是否允許這種類型的工件
- updatePolicy:表示多久嘗試更新一次。可選值有always、daily、interval:minutes(表示每多久更新一次)和never。
- checksumPolicy:當Maven在部署項目到倉庫的時候會連同校驗文件一起提交,checksumPolicy表示當這個校驗文件缺失或不正確的時候該如何處理,可選項有ignore、fail和warn。
1.4 pluginRepositories:在Maven中有兩種類型的倉庫,一種是存儲工件的倉庫,另一種就是存儲plugin插件的倉庫。pluginRepositories的定義和repositories的定義類似,它表示Maven在哪些地方可以找到所需要的插件。
activeProfiles:底包含一系列的activeProfile元素,表示對於所有的pom都處於活躍狀態的profile。如:
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>