安裝Maven以前要確保已經安裝好了jdk,而且配置好了環境變量JAVA_HOME。具體安裝步驟以下:html
通過以上幾步以後Maven就安裝成功了。接下來咱們能夠在命令窗口使用mvn --version來驗證一下Maven是否安裝成功。如能正確輸出Maven的安裝版本,則表示它安裝成功了。linux
在Maven中提供了一個settings.xml文件來定義Maven的全局環境信息。這個文件會存在於Maven的安裝目錄的conf子目錄下面,或者是用戶家目錄的.m2子目錄下面。咱們能夠經過這個文件來定義本地倉庫、遠程倉庫和聯網使用的代理信息等。apache
其實相對於多用戶的PC機而言,在Maven安裝目錄的conf子目錄下面的settings.xml纔是真正的全局的配置。而用戶家目錄的.m2子目錄下面的settings.xml的配置只是針對當前用戶的。當這兩個文件同時存在的時候,那麼對於相同的配置信息用戶家目錄下面的settings.xml中定義的會覆蓋Maven安裝目錄下面的settings.xml中的定義。用戶家目錄下的settings.xml文件通常是不存在的,可是Maven容許咱們在這裏定義咱們本身的settings.xml,若是須要在這裏定義咱們本身的settings.xml的時候就能夠把Maven安裝目錄下面的settings.xml文件拷貝到用戶家目錄的.m2目錄下,而後改爲本身想要的樣子。windows
先來看一個基本的settings.xml的樣子:服務器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> 5 6 <localRepository>D:\\develop\\mavenRepository</localRepository> 7 <interactiveMode>true</interactiveMode> 8 <offline>false</offline> 9 <pluginGroups> 10 11 </pluginGroups> 12 13 <proxies> 14 <proxy> 15 <id>optional</id> 16 <active>true</active> 17 <protocol>http</protocol> 18 <username>proxyuser</username> 19 <password>proxypass</password> 20 <host>proxy.host.net</host> 21 <port>80</port> 22 <nonProxyHosts>local.net|some.host.com</nonProxyHosts> 23 </proxy> 24 </proxies> 25 26 <servers> 27 <server> 28 <id>deploymentRepo</id> 29 <username>repouser</username> 30 <password>repopwd</password> 31 </server> 32 </servers> 33 34 <mirrors> 35 <mirror> 36 <id>mirrorId</id> 37 <mirrorOf>repositoryId</mirrorOf> 38 <name>Human Readable Name for this Mirror.</name> 39 <url>http://my.repository.com/repo/path</url> 40 </mirror> 41 </mirrors> 42 43 <profiles> 44 <profile> 45 <id>jdk-1.5</id> 46 <activation> 47 <jdk>1.5</jdk> 48 </activation> 49 <repositories> 50 <repository> 51 <id>jdk15</id> 52 <name>jdk1.5</name> 53 <url>http://www.myhost.com/maven/jdk15</url> 54 <layout>default</layout> 55 <snapshotPolicy>always</snapshotPolicy> 56 </repository> 57 </repositories> 58 </profile> 59 </profiles> 60 <activeProfiles> 61 <activeProfile>jdk-1.5</activeProfile> 62 </activeProfiles> 63 </settings>
settings.xml中主要包括如下元素:maven
localRepository:表示Maven用來在本地儲存信息的本地倉庫的目錄。默認是用戶家目錄下面的.m2/repository目錄。ide
interactiveMode:表示是否使用交互模式,默認是true;若是設爲false,那麼當Maven須要用戶進行輸入的時候,它會使用一個默認值。this
offline:表示是否離線,默認是false。這個屬性表示在Maven進行項目編譯和部署等操做時是否容許Maven進行聯網來下載所須要的信息。url
pluginGroups:在pluginGroups元素下面能夠定義一系列的pluginGroup元素。表示當經過plugin的前綴來解析plugin的時候到哪裏尋找。pluginGroup元素指定的是plugin的groupId。默認狀況下,Maven會自動把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。spa
proxies:其下面能夠定義一系列的proxy子元素,表示Maven在進行聯網時須要使用到的代理。當設置了多個代理的時候第一個標記active爲true的代理將會被使用。下面是一個使用代理的例子:
1 <proxies> 2 <proxy> 3 <id>xxx</id> 4 <active>true</active> 5 <protocol>http</protocol> 6 <username>用戶名</username> 7 <password>密碼</password> 8 <host>代理服務器地址</host> 9 <port>代理服務器的端口</port> 10 <nonProxyHosts>不使用代理的主機</nonProxyHosts> 11 </proxy> 12 </proxies>
servers:其下面能夠定義一系列的server子元素,表示當須要鏈接到一個遠程服務器的時候須要使用到的驗證方式。這主要有username/password和privateKey/passphrase這兩種方式。如下是一個使用servers的示例:
1 <servers> 2 <server> 3 <id>id</id> 4 <username>用戶名</username> 5 <password>密碼</password> 6 </server> 7 </servers>
mirrors:用於定義一系列的遠程倉庫的鏡像。咱們能夠在pom中定義一個下載工件的時候所使用的遠程倉庫。可是有時候這個遠程倉庫會比較忙,因此這個時候人們就想着給它建立鏡像以緩解遠程倉庫的壓力,也就是說會把對遠程倉庫的請求轉換到對其鏡像地址的請求。每一個遠程倉庫都會有一個id,這樣咱們就能夠建立本身的mirror來關聯到該倉庫,那麼之後須要從遠程倉庫下載工件的時候Maven就能夠從咱們定義好的mirror站點來下載,這能夠很好的緩解咱們遠程倉庫的壓力。在咱們定義的mirror中每一個遠程倉庫都只能有一個mirror與它關聯,也就是說你不能同時配置多個mirror的mirrorOf指向同一個repositoryId。
看如下是一個使用mirrors的例子:
1 <mirrors> 2 <mirror> 3 <id>mirrorId</id> 4 <mirrorOf>repositoryId</mirrorOf> 5 <name>定義一個容易看懂的名稱 </name> 6 <url>http://my.repository.com/repo/path</url> 7 </mirror> 8 </mirrors>
1》 id:是用來區別mirror的,全部的mirror不能有相同的id
2》 mirrorOf:用來表示該mirror是關聯的哪個倉庫,其值爲其關聯倉庫的id。當要同時關聯多個倉庫時,這多個倉庫之間能夠用逗號隔開;當要關聯全部的倉庫時,可使用「*」表示;當要關聯除某一個倉庫之外的其餘全部倉庫時,能夠表示爲「*,!repositoryId」;當要關聯不是localhost或用file請求的倉庫時,能夠表示爲「external:*」。
3》 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元素來指定的。
看下面一個例子:
1 <profiles> 2 <profile> 3 <id>test</id> 4 <activation> 5 <activeByDefault>false</activeByDefault> 6 <jdk>1.6</jdk> 7 <os> 8 <name>Windows 7</name> 9 <family>Windows</family> 10 <arch>x86</arch> 11 <version>5.1.2600</version> 12 </os> 13 <property> 14 <name>mavenVersion</name> 15 <value>2.0.3</value> 16 </property> 17 <file> 18 <exists>${basedir}/file2.properties</exists> 19 <missing>${basedir}/file1.properties</missing> 20 </file> 21 </activation> 22 ... 23 </profile> 24 </profiles>
在上面這段代碼中,當全部的約束條件都知足的時候就會激活這個profile。
1》 jdk:表示當jdk的版本知足條件的時候激活,在這裏是1.6。這裏的版本還能夠用一個範圍來表示,如
<jdk>[1.4,1.7)</jdk>表示1.四、1.5和1.6知足;
<jdk>[1.4,1.7]</jdk>表示1.四、1.五、1.6和1.7知足;
2》 os:表示當操做系統知足條件的時候激活。
3》 property:property是鍵值對的形式,表示當Maven檢測到了這樣一個鍵值對的時候就激活該profile。
(1)下面的示例表示當存在屬性hello的時候激活該profile。
1 <property> 2 <name>hello</name> 3 </property>
(2)下面的示例表示當屬性hello的值爲world的時候激活該profile。
1 <property> 2 <name>hello</name> 3 <value>world</value> 4 </property>
這個時候若是要激活該profile的話,能夠在調用Maven指令的時候加上參數hello並指定其值爲world,如:
mvn compile –Dhello=world
1》 file:表示當文件存在或不存在的時候激活,exists表示存在,missing表示不存在。以下面的例子表示當文件hello/world不存在的時候激活該profile。
1 <profile> 2 <activation> 3 <file> 4 <missing>hello/world</missing> 5 </file> 6 </activation> 7 </profile>
2》 activeByDefault:當其值爲true的時候表示若是沒有其餘的profile處於激活狀態的時候,該profile將自動被激活。
(2)properties:用於定義屬性鍵值對的。當該profile是激活狀態的時候,properties下面指定的屬性均可以在pom.xml中使用。
(3)repositories:用於定義遠程倉庫的,當該profile是激活狀態的時候,這裏面定義的遠程倉庫將做爲當前pom的遠程倉庫。
1 <repositories> 2 <repository> 3 <id>codehausSnapshots</id> 4 <name>Codehaus Snapshots</name> 5 <releases> 6 <enabled>false</enabled> 7 <updatePolicy>always</updatePolicy> 8 <checksumPolicy>warn</checksumPolicy> 9 </releases> 10 <snapshots> 11 <enabled>true</enabled> 12 <updatePolicy>never</updatePolicy> 13 <checksumPolicy>fail</checksumPolicy> 14 </snapshots> 15 <url>http://snapshots.maven.codehaus.org/maven2</url> 16 <layout>default</layout> 17 </repository> 18 </repositories>
1》 releases、snapshots:這是對於工件的類型的限制。
2》 enabled:表示這個倉庫是否容許這種類型的工件
3》 updatePolicy:表示多久嘗試更新一次。可選值有always、daily、interval:minutes(表示每多久更新一次)和never。
4》 checksumPolicy:當Maven在部署項目到倉庫的時候會連同校驗文件一塊兒提交,checksumPolicy表示當這個校驗文件缺失或不正確的時候該如何處理,可選項有ignore、fail和warn。
(4)pluginRepositories:在Maven中有兩種類型的倉庫,一種是存儲工件的倉庫,另外一種就是存儲plugin插件的倉庫。pluginRepositories的定義和repositories的定義相似,它表示Maven在哪些地方能夠找到所須要的插件。
activeProfiles:底包含一系列的activeProfile元素,表示對於全部的pom都處於活躍狀態的profile。如:
1 <activeProfiles> 2 <activeProfile>alwaysActiveProfile</activeProfile> 3 <activeProfile>anotherAlwaysActiveProfile</activeProfile> 4 </activeProfiles>
參考轉載:haohaoxuexi.iteye.com/blog/1827778