1.http://maven.apache.org/
1.下載apache-maven-3.1.1-bin.zip,解壓至C盤根目錄
2.添加環境變量至path:C:\apache-maven-3.1.1\bin
3.cmd命令下輸入:mvn -version/mvn-v
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:2
2+0800)
Maven home: C:\apache-maven-3.1.1\bin\..
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_51\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"
2.mvn -help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
3.maven項目結構
${basedir} 存放 pom.xml(Project Object Model)和所有的子目錄
${basedir}/src/main/java 項目的 java源代碼
${basedir}/src/main/resources 項目的資源,比如說 property文件
${basedir}/src/test/java 項目的測試類,比如說 JUnit代碼
${basedir}/src/test/resources 測試使用的資源
編譯后 的 classes 會放在 ${basedir}/target/classes 下面, JAR會放在 ${basedir}/target
-->慣例優于配置
4.Maven倉庫
1.倉庫分類:本地倉庫和遠程倉庫。Maven根據坐標尋找構件的時候,它先會查看本地倉庫,如果本地倉庫存在構件,則直接使用;如果沒有,則從遠程倉庫查找,找到后,下載到本地
2.默認情況下,每個用戶在自己的用戶目錄下都有一個路徑名為.m2/repository/的倉庫目錄。可以自定義本地倉庫的地址.修改路徑時,先將$M2_HOME/conf/settings.xml文件復制到~/.m2/settings.xml,再對后者進行編輯,設置localRepository元素的值為想要的倉庫地址
3.遠程中央倉庫的地址為 http://repo1.maven.org/
4.私服/鏡像
5.settings.xml
5.POM
1.Maven坐標
1.groupId:組織標識.如com.landon,在m2_repository目錄下:com/landon
2.artifactId:項目名稱,如mavs,在m2_repository目錄下:com/landon/mavs
3.version:版本號,如1.0.0,在m2_repository目錄下:com/landon/mavs/1.0.0
4.packaging:打包的格式,如jar,war
2.依賴關系dependencies(項目中依賴的jar)
dependency屬性
1.groupId/artifactId/version 依賴的具體工程
2.scope 依賴范圍 compile/test/runtime/provided/system
3.optional 去除依賴傳遞
4.exclusions 排除依賴
3.聚合關系
1.通過一個父模塊將所有的要構建模塊整合起來,將父模塊的打包類型聲明為 POM,通過 <modules> 將各模塊集中到父 POM.<module></module> 中間的內容為子模塊工程名的相對路徑.
2.Maven 會首先解析聚合模塊的 POM 文件,分析要構建的模塊,并通過各模塊的依賴關系計算出模塊的執行順序,根據這個潛在的關系依次構建模塊。將各子模塊聚合到父模塊中后,我們就可以對父模塊進行一次構建命令來完成全部模塊的構建
4.繼承關系
1.通過構建父模塊將子模塊共用的依賴,插件等進行統一聲明
2.parent
5.插件
1.Maven本質上是一個插件框架,它的核心并不執行任何具體的構建任務,所有這些任務都交給插件來完成,例如編譯源代碼是由maven-compiler-plugin完成的
2.每個任務對應了一個插件目標(goal),每個插件會有一個或者多個目標,例如maven-compiler-plugin的compile目標用來編譯位于src/main/java/目錄下的主源碼,testCompile目標用來編譯位于src/test/java/目錄下的測試源碼
3.maven-antrun-plugin能讓用戶在Maven項目中運行Ant任務
4.plugins/plugin
6.構建
1.build
2.結合plugins/resources
3.profile(多環境構建)
6.maven屬性(和ant差不多)
1.內置屬性。這種屬性跟 Maven Project 自身有關,比如要引入當前 Project 的版本信 息,那么只需要在使用的位置引用 ${version} 就行了。
2.Setting 屬性。上文中已經提到 Maven 自身有一個 settings.xml 配置文件,它里面含有包括倉庫,代理服務器等一些配置信息,利用 ${settings.somename} 就可以得到文件里相應元素的值。
3.POM 屬性。這種屬性對應 POM 文件中對應元素的值,例如 ${project.groupId} 對應了 <groupId></groupId> 中的值,${project.artifactId} 對應了 <artifactId> </ artifactId > 中的值。
4.系統環境變量。可以使用 env.${name} 來獲得相應 name 對應的環境變量的值,例如 ${env.JAVA_HOME} 得到的就是 JAVA_HOME 的環境變量值。
5.用戶自定義變量。這種類型的變量是使用最頻繁和廣泛的變量,完全由用戶自己定義。在 POM 文件中加入 <properties> 元素并將自定義屬性作為其子元素
7.生命周期和階段
Maven有三套相互獨立的生命周期,分別是clean、default和site。每個生命周期包含一些階段(phase),階段是有順序的,后面的階段依賴于前面的階段。
a、clean生命周期:清理項目,包含三個phase。
1)pre-clean:執行清理前需要完成的工作
2)clean:清理上一次構建生成的文件
3)post-clean:執行清理后需要完成的工作
b、default生命周期:構建項目,重要的phase如下。
1)validate:驗證工程是否正確,所有需要的資源是否可用。
2)compile:編譯項目的源代碼。
3)test:使用合適的單元測試框架來測試已編譯的源代碼。這些測試不需要已打包和布署。
4)Package:把已編譯的代碼打包成可發布的格式,比如jar。
5)integration-test:如有需要,將包處理和發布到一個能夠進行集成測試的環境。
6)verify:運行所有檢查,驗證包是否有效且達到質量標準。
7)install:把包安裝到maven本地倉庫,可以被其他工程作為依賴來使用。
8)Deploy:在集成或者發布環境下執行,將最終版本的包拷貝到遠程的repository,使得其他的開發者或者工程可以共享。
c、site生命周期:建立和發布項目站點,phase如下
舉例如下:
1、mvn clean
調用clean生命周期的clean階段,實際執行pre-clean和clean階段
2、mvn test
調用default生命周期的test階段,實際執行test以及之前所有階段
3、mvn clean install
調用clean生命周期的clean階段和default的install階段,實際執行pre-clean和clean,install以及之前所有階段
build lifecycle & build phase & goal
maven有一套build的生命周期,是按照一套順序走下來的,這一套順序就叫一個生命周期(lifecycle)。maven內置三種生命周期:default, clean 和 site。一個生命周期分為多個build phase,下面是default生命周期全部的build phase:
- validate:validate the project is correct and all necessary information is available.
- initialize:initialize build state, e.g. set properties or create directories.
- generate-sources:generate any source code for inclusion in compilation.
- process-sources:process the source code, for example to filter any values.
- generate-resources:generate resources for inclusion in the package.
- process-resources:copy and process the resources into the destination directory, ready for packaging.
- compile:compile the source code of the project.
- process-classes:post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
- generate-test-sources:generate any test source code for inclusion in compilation.
- process-test-sources:process the test source code, for example to filter any values.
- generate-test-resources:create resources for testing.
- process-test-resources:copy and process the resources into the test destination directory.
- test-compile:compile the test source code into the test destination directory
- process-test-classes:post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
- test:run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
- prepare-package:perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
- package:take the compiled code and package it in its distributable format, such as a JAR.
- pre-integration-test:perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
- integration-test:process and deploy the package if necessary into an environment where integration tests can be run.
- post-integration-test:perform actions required after integration tests have been executed. This may including cleaning up the environment.
- verify:run any checks to verify the package is valid and meets quality criteria.
- install:install the package into the local repository, for use as a dependency in other projects locally.
- deploy:done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
這些build phase是按照順序執行的,如果執行后面的build phase,前面的build phase 也會被執行。例如如果執行:
mvn deploy
前面的install、verify一直到validate這些build phase都會執行。
每一個build phase是由goal組成的,一個goal其實就是一個任務,一個goal可以關聯到一個build phase也可以不關聯到任何build phase 。 不關聯到任何phase的goal是可以獨立執行的,例如:
mvn clean dependency:copy-dependencies package
上面的命令會導致先執行clean這個phase,然后拷貝依賴項,最后打包。注意,這里clean這個goal是clean這個lifecycle里面的一個goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起執行的。 如果一個goal被綁定到多個phase上,那么goal就會被執行多次。
phase的順序是已經固定的,如果一個phase沒有綁定到任何goal,那么phase就不會被執行。 一個goal可以通過兩種方式綁定到一個phase,一個是指定packaging,另一個就是plugin。
packaging&plugin
plugin就是用來向maven提供goal的。一個plugin里面可以有多個goal,這就是為什么我們在指明goal時,前面會用一個冒號與plugin的名字。
一個plugin自己可以指定自己的goal綁定到哪個lifecycle的哪一個Phase上,另外也可以配置一個goal綁定到哪個phase上。可以在pom.xml里面配置。 看兩個配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < plugin >
< groupId >org.codehaus.modello</ groupId >
< artifactId >modello-maven-plugin</ artifactId >
< version >1.4</ version >
< executions >
< execution >
< configuration >
< models >
< model >src/main/mdo/maven.mdo</ model >
</ models >
< version >4.0.0</ version >
</ configuration >
< goals >
< goal >java</ goal >
</ goals >
</ execution >
</ executions >
</ plugin >
|
這個就在當前的lifecycle里面添加了一個名字叫java的goal,這goal會根據自己的配置去綁定到一個phase,在phase執行的時候這個goal會執行。并且在這個配置里面,可以指定多個execution來讓這個goal執行多次。
看另一個示例配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 | < plugin >
< groupId >com.mycompany.example</ groupId >
< artifactId >display-maven-plugin</ artifactId >
< version >1.0</ version >
< executions >
< execution >
< phase >process-test-resources</ phase >
< goals >
< goal >time</ goal >
</ goals >
</ execution >
</ executions >
</ plugin >
|
這個名為time的goal把自己綁定到了process-test-resource這個phase上。
在默認情況下,并不是所有的phase都綁定了goal,比如clean這個lifecycle是有三個phase的,但是只有其中的一個名為clean的phase默認綁定了一個clean:clean goal,其它兩個phase默認沒有綁定任何goal。
之前已經提到過packaging,在pom.xml可以指定packaging,每種packaging都設定了一組phase和goal之間的綁定關系。在default lifecycle下,當packaging為 ejb/ejb3/jar/par/rar/war 其中之一的值的時候,只有以下的phase綁定了goal,具體如下:
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar |
install | install:install |
deploy | deploy:deploy |
總結
首先搞清楚maven的project的目錄結構,然后理解maven的lifecycle,lifecycle是由build phase組成,每一個build phase會綁定到goal。goal是由plugin提供的。 每一種packaging的值都表明了一定的phase和goal之間的綁定關系。
另外一個很重要的就是dependency,我們要在項目中引用一個依賴,只需要在pom.xml指定依賴的名字和版本,maven會自動去遠程的repository下載,然后放到本地的repository里面,這樣以后所有的project都可以共用
其它細節可以參考http://maven.apache.org/guides/index.html。
8.查找依賴jar
http://search.maven.org/
你懂得
9.參考:
1.http://maven.apache.org/settings.html
2.http://maven.apache.org/pom.html
3.http://maven.apache.org/guides/index.html
posted on 2014-02-03 00:02
landon 閱讀(2194)
評論(2) 編輯 收藏 所屬分類:
Utils