Maven的安裝
安裝Maven之前要確保已經安裝好了jdk,并且配置好了環境變量JAVA_HOME。具體安裝步驟如下:
1. 從apache網上下載maven項目的壓縮包。下載地址為:http://maven.apache.org/download.html。比如現在最新的Maven版本是3.0.4,那么我下載好的安裝文件就是apache-maven-3.0.4.zip。
2. 將下載后的壓縮包解壓到Maven的安裝目錄,比如說是D:\\develop,那么解壓后就是D:\\develop\\apache-maven-3.0.4。
3. 添加環境變量M2_HOME,其值為Maven的家目錄,如D:\\develop\\apache-maven-3.0.4。
4.添加環境變量M2,其值將為maven安裝目錄的bin目錄,即D:\\develop\\apache-maven-3.0.4\\bin,對于windows系統也可以使用%M2_HOME%\\bin,對于linux系統也可以使用$M2_HOME/bin。
5.將環境變量M2加入到PATH變量中,對于windows系統,可以在PATH變量的值后面加上“;%M2%”,對于linux系統,可以使用“export path=$path:$M2”
6.還有一個可選的環境變量MAVEN_OPTS,該環境變量主要是配置Maven在使用jdk的時候指定JVM屬性的。如指定其值為“-Xms256m -Xmx512m”。
經過以上幾步之后Maven就安裝成功了。接下來我們可以在命令窗口使用mvn --version來驗證一下Maven是否安裝成功。如能正確輸出Maven的安裝版本,則表示它安裝成功了。
當我們創建一個簡單的Maven項目后(只需要在pom.xml配置好依賴),運行mvn clean install就可以把項目構建好,不需要我們手工下載任何jar,這全靠中央倉庫的存在,它會自動從倉庫下載。這個倉庫的定義是在${M2_HOME}/lib/maven-2.0.10-uber.jar 里面。你可以在里面找到\org\apache\maven\project\pom-4.0.0.xml 這個文件,在這個文件里面定義了默認中央倉庫的地址:
1
2
3
4
5
6
7
8
9
10
11
|
< repositories > < repository > < id > central</ id > < name > Maven Repository Switchboard</ name > < layout > default</ layout > < url > http://repo1.maven.org/maven2</ url > < snapshots > < enabled > false</ enabled > </ snapshots > </ repository > </ repositories > |
如果你想覆蓋中央倉庫的默認地址,那么這里我們就會使用的鏡像了,還在setting.xml里面配置:
1
2
3
4
5
6
7
8
9
10
11
12
|
< settings > … < mirrors > < mirror > < id > maven-net-cn</ id > < name > Maven China Mirror</ name > < url > http://maven.net.cn/content/groups/public/</ url > < mirrorOf > central</ mirrorOf > </ mirror > </ mirrors > … </ settings > |
,表示只為central倉庫做鏡像,如果想為所有的倉庫做鏡像那么可以改為: *
Maven配置詳解
maven2 比起maven1 來說,需要配置的文件少多了,主要集中在pom.xml和settings.xml中。
先來說說settings.xml,settings.xml對于maven來說相當于全局性的配置,用于所有的項目。在maven2中存在兩個settings.xml,一個位于maven2的安裝目錄conf下面,作為全局性配置。對于團隊設置,保持一致的定義是關鍵,所以maven2/conf下面的settings.xml就作為團隊共同的配置文件。保證所有的團隊成員都擁有相同的配置。當然對于每個成員,都需要特殊的自定義設置,如用戶信息,所以另外一個settings.xml就作為本地配置。默認的位置為:user.dir/.m2/settings.xml目錄中({user.dir} 指windows 中的用戶目錄)。
settings.xml基本結構如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< settings xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> < localRepository /> < interactiveMode /> < usePluginRegistry /> < offline /> < pluginGroups /> < servers /> < mirrors /> < proxies /> < profiles /> < activeProfiles /> </ settings > |
簡單介紹一下幾個主要的配置因素:
localRepository
表示本地庫的保存位置,也就是maven2主要的jar保存位置,默認在${user.dir}/.m2/repository,如果需要另外設置,就換成其他的路徑。
offline
如果不想每次編譯,都去查找遠程中心庫,那就設置為true。當然前提是你已經下載了必須的依賴包。
Servers
在POM中的 distributionManagement元素定義了開發庫。然而,特定的username和pwd不能使用于pom.xml,所以通過此配置來保存server信息
1
2
3
4
5
6
7
8
9
10
11
12
|
< servers > < server > < id >server001</ id > < username >my_login</ username > < password >my_password</ password > < privateKey >${usr.home}/.ssh/id_dsa</ privateKey > < passphrase >some_passphrase</ passphrase > < filePermissions >664</ filePermissions > < directoryPermissions >775</ directoryPermissions > < configuration ></ configuration > </ server > </ servers > |
- id:server 的id,用于匹配distributionManagement庫id,比較重要。
- username, password:用于登陸此服務器的用戶名和密碼
- privateKey, passphrase:設置private key,以及passphrase
- filePermissions, directoryPermissions:當庫文件或者目錄創建后,需要使用權限進行訪問。參照unix文件許可,如664和775
Mirrors
表示鏡像庫,指定庫的鏡像,用于增加其他庫
1
2
3
4
5
6
7
8
|
< mirrors > < mirror > < id >planetmirror.com</ id > < name >PlanetMirror Australia</ name > < url >http://downloads.planetmirror.com/pub/maven2</ url > < mirrorOf >central</ mirrorOf > </ mirror > </ mirrors > |
- id,name:唯一的標志,用于區別鏡像
- url:鏡像的url
- mirrorOf:此鏡像指向的服務id
Proxies
此設置,主要用于無法直接訪問中心的庫用戶配置。
1
2
3
4
5
6
7
8
9
10
11
12
|
< proxies > < proxy > < id >myproxy</ id > < active >true</ active > < protocol >http</ protocol > < host >proxy.somewhere.com</ host > < port >8080</ port > < username >proxyuser</ username > < password >somepassword</ password > < nonProxyHosts >*.google.com|ibiblio.org</ nonProxyHosts > </ proxy > </ proxies > |
- id:代理的標志
- active:是否激活代理
- protocol, host, port:protocol://host:port 代理
- username, password:用戶名和密碼
- nonProxyHosts: 不需要代理的host
Profiles
類似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素
剛開始接觸的時候,可能會比較迷惑,其實這是maven2中比較強大的功能。從字面上來說,就是個性配置。
單獨定義profile后,并不會生效,需要通過滿足條件來激活。
repositories 和pluginRepositories
定義其他開發庫和插件開發庫。對于團隊來說,肯定有自己的開發庫。可以通過此配置來定義。
如下的配置,定義了本地開發庫,用于release 發布。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
< repositories > < repository > < id >repo-local</ id > < name >Internal 開發庫</ name > < url >http://192.168.0.2:8082/repo-local</ url > < releases > < enabled >true</ enabled > < updatePolicy >never</ updatePolicy > < checksumPolicy >warn</ checksumPolicy > </ releases > < snapshots > < enabled >false</ enabled > </ snapshots > < layout >default</ layout > </ repository > </ repositories > < pluginRepositories > < pluginRepository > < id >repo-local</ id > < name >Internal 開發庫</ name > < url >http://192.168.0.2:8082/repo-local</ url > < releases > < enabled >true</ enabled > < updatePolicy >never</ updatePolicy > < checksumPolicy >warn</ checksumPolicy > </ releases > < snapshots > < enabled >false</ enabled > </ snapshots > < layout >default</ layout > </ pluginRepository > </ pluginRepositories > |
releases, snapshots:每個產品的版本的Release或者snapshot(注:release和snapshot的區別,release一般是比較穩定的版本,而snapshot基本上不穩定,只是作為快照)
properties
maven 的properties作為placeholder值,如ant的properties。
包括以下的5種類型值:
1. env.X,返回當前的環境變量
2. project.x:返回pom中定義的元素值,如project.version
3. settings.x:返回settings.xml中定義的元素
4. java 系統屬性:所有經過java.lang.System.getProperties()返回的值
5. x:用戶自己設定的值
Activation
用于激活此profile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
< activation > < activeByDefault >false</ activeByDefault > < jdk >1.5</ jdk > < os > < name >Windows XP</ 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 > |
- jdk:如果匹配指定的jdk版本,將會激活
- os:操作系統
- property:如果maven能檢測到相應的屬性
- file: 用于判斷文件是否存在或者不存在
除了使用activation來激活profile,同樣可以通過activeProfiles來激活
Active Profiles
表示激活的profile,通過profile id來指定。
1
2
3
|
< activeProfiles > < activeProfile >env-test</ activeProfile > 指定的profile id </ activeProfiles > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/benben683280/article/details/78798922