在項目裏用了快一年的maven了,最近忽然發現maven項目在eclipse中build時很是慢,由於常常用clean install命令來build項目,也沒有管那麼多,但最近實在受不了烏龜同樣的build速度,因而下定決心再看看《maven實戰》吧,
對於我來講,maven最主要的做用有兩個方面,一個是對jar包的依賴解決功能,本身管理jar包,另外一個功能就是項目的構建,打包部署。如今我以爲最重要的仍是maven的生命週期和插件機制,下面就來總結一下吧。css
Maven 官網:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.htmlhtml
對於maven的生命週期來講,共有三個相互獨立的生命週期,分別是clean、default、site。clean生命週期目的是清理項目,default生命週期目的是構建項目,而site生命週期目的是創建項目站點。
每一個生命週期分別包含一些階段,這些階段是有順序的,而且後面的階段依賴於前面的階段。如clean生命週期包含pre-clean、clean和post-clean三個階段,若是執行clean階段,則會先執行pre-clean階段。
較之於生命週期階段有先後依賴關係,三套生命週期自己是相互獨立的,用戶能夠僅調用clean生命週期的某個階段,也能夠不執行clean週期,而直接執行default生命週期的某幾個階段。java
clean生命週期包含三個階段,主要負責清理項目,以下:web
pre-clean | executes processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | executes processes needed to finalize the project cleaning |
default生命週期定義了真正構建時所須要執行的全部步驟,包含的階段以下:apache
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. |
siet生命週期的目的是創建和發佈項目站點,maven可以基於POM所包含的信息,自動生成一個友好的站點,方便團隊交流和發佈項目信息,包含的階段以下:oracle
pre-site | executes processes needed prior to the actual project site generation |
site | generates the project's site documentation |
post-site | executes processes needed to finalize the site generation, and to prepare for site deployment |
site-deploy | deploys the generated site documentation to the specified web server |
從命令行執行maven任務的最主要方式就是調用maven的生命週期階段。須要注意的是,各個生命週期是相互獨立的,而一個生命週期的階段是有先後依賴關係的。例子以下:
一、$mvn clean :該命令調用clean生命週期的clean階段。實際執行的階段爲clean生命週期的pre-clean和clean階段。
二、$mvn test:該命令調用default生命週期的test階段。實際調用的是default生命週期的validate、initialize等,直到test的全部階段。
三、$mvn clean install:該命令調換用clean生命週期的clean階段和default生命週期的instal階段。app
maven的核心僅僅定義了抽象的生命週期,具體的任務是交由插件完成的,插件以獨立的形式存在。
對於插件自己,爲了可以複用代碼,它每每可以完成多個任務。如maven-dependency-plugin有十多個目標,每一個目標對應了一個功能,如 dependency:analyze、 dependency:tree和dependency:list。這是一種通用的寫法,冒號前面是插件前綴,後面是該插件的目標。eclipse
maven的生命週期與插件相互綁定,用以完成實際的構建任務。具體而言,是生命週期的階段與插件的目標相互綁定,已完成某個具體的構建任務。例如項目編譯這一任務,它對應了default生命週期的compile階段,而maven-compiler-plugin這一插件的compile目標可以完成該任務,所以將他們綁定。webapp
maven在覈心爲一些主要的生命週期接到綁定了不少插件的目標,以下:clean和site生命週期相對簡單。maven
clean | clean:clean |
site | site:site |
site-deploy | site:deploy |
default生命週期與插件目標的綁定關係有點複雜一些。這是由於對於任何項目來講,例如jar項目和war項目,他們的項目清理和站點生成任務是同樣的,不過構建過程會有區別。例如jar項目須要打成jar包,而war項目須要打成war包。
因爲項目的打包類型會影響構建的具體過程,所以,default生命週期的階段與插件目標的綁定關係有項目打包類型所決定的,打包類型是經過pom中的packaging元素定義的。最多見的打包類型是jar,它也是默認的打包類型。基於該打包類型,default生命週期的內置綁定關係以下:
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install | install:install |
deploy | deploy:deploy |
除了內置綁定覺得,用戶還可以本身選擇獎某個插件目標綁定到生命週期的某個階段以執行更多更特點的任務。
<!-- 自動複製資源文件件到根目錄 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <includeEmptyDirs>true</includeEmptyDirs> <encoding>GBK</encoding> <nonFilteredFileExtensions> <nonFilteredFileExtension>exe</nonFilteredFileExtension> <nonFilteredFileExtension>zip</nonFilteredFileExtension> <nonFilteredFileExtension>vbs</nonFilteredFileExtension> <nonFilteredFileExtension>sh</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <includeEmptyDirs>true</includeEmptyDirs> <outputDirectory>${project.build.directory}</outputDirectory> <excludes> <exclude>agentmanager.jsmooth</exclude> <exclude>assembly.xml</exclude> </excludes> <resources> <resource> <directory>src/main/resources/</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
如上圖定義了一個id爲copy-resources的任務,綁定到default生命週期的validate階段,綁定的插件爲maven-resources-plugin,插件目標爲copy-resources。即用插件的copy-resources功能來實現項目資源文件的拷貝。
<!-- 自動複製maven依賴包到lib目錄 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>lib</outputDirectory> </configuration> </execution> </executions> </plugin>
同上,定義了一個id爲copy的任務,利用插件maven-dependency-plugin的copy-dependencies目標綁定到default生命週期的install階段,來實現項目依賴的jar包的自動複製。
當插件目標被綁定到不一樣的生命週期階段時候,其執行順序會有生命週期階段的前後順序決定的。若是多個目標被綁定到同一個階段,他們的執行順序是由插件聲明的前後順序決定目標的執行順序。
用戶能夠配置插件目標的參數,進一步調整插件目標所執行的任務。
如 $mvn install -Dmaven.test.skip=true 的意義即跳過測試步驟。
參數-D的java自帶的,其功能是經過命令行設置一個java系統屬性,maven簡單地重用了該參數以實現插件參數的配置。
如項目編譯使用1.6版本的源文件,生成與JVM1.6兼容的字節碼文件,以下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin>
$mvn help:describe-Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1 來獲取插件的詳細信息
能夠簡化爲:
$mvn help:describe-Dplugin=compiler
若是僅僅描述插件目標的信息,能夠加上goal參數:
$mvn help:describe-Dplugin=compiler-Dgoal=compile
若是想輸出更詳細的信息,能夠加上detail參數:
$mvn help:describe-Dplugin=compiler-Ddetail
一個優秀的構建系統必須足夠靈活,應該可以讓項目在不一樣的環境下都能成功構建。maven爲了支持構建的靈活性,內置了三大特性,即:屬性、profile和資源過濾。
maven屬性分6類:
一、內置屬性:如${basedir}表示項目根目錄,${version}表示項目版本
二、POM屬性:用戶能夠引用pom文件中對應的值。如:
${basedir} 項目根目錄
${project.build.directory} 構建目錄,缺省爲target
${project.build.outputDirectory} 構建過程輸出目錄,缺省爲target/classes
${project.build.finalName} 產出物名稱,缺省爲${project.artifactId}-${project.version}
${project.packaging} 打包類型,缺省爲jar
${project.xxx} 當前pom文件的任意節點的內容
三、自定義屬性:用戶能夠在pom的<properties>元素下自定義maven屬性。
四、setting屬性:用戶可使用以settings開頭的屬性引用settings.xml中xml元素的值,如${settings.localRepository}指向用戶本地倉庫的地址。
五、java系統屬性:maven可使用當前java系統的屬性,如${user.home}指向了用戶目錄。
六、環境變量屬性:全部環境變量均可以使用以env.開頭的屬性。如:${env.JAVA_HOE}。
這裏所謂的資源:也就就是指src/main/resources和src/test/resources文件下的全部文件,默認狀況下,這些文件會被複制到classpath下面,即target/classes下面。
所謂資源過濾,就是過濾這些文件夾下面的文件裏面的內容,看裏面的maven變量是否須要替換。默認狀況下,只有pom.xml裏面的變量纔會被替換,資源文件是不會被過濾的,可是能夠設置,以下:
<build> <finalName>agentmanager</finalName> <sourceDirectory>src/main/java</sourceDirectory> <resources> <!-- 控制資源文件的拷貝 --> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/jre.zip</exclude> <exclude>**/jre.tar</exclude> <exclude>agentmanager.jsmooth</exclude> <exclude>assembly.xml</exclude> </excludes> <targetPath>${project.build.directory}</targetPath> </resource> <resource> <directory>src/main/resources/conf</directory> <targetPath>${basedir}/conf</targetPath> <filtering>true</filtering> </resource> </resources> </build>
如jdbc.properties
jdbc.driverClassName=${db.driver} jdbc.url=${db.url} jdbc.username=${db.user} jdbc.password=${db.pwd}
profile文件
<profiles> <profile> <id>dev</id> <properties> <db.driver>oracle.jdbc.driver.OracleDriver</db.driver> <db.url>jdbc:oracle:thin:@10.252.48.3:1521:dbname</db.url> <db.user>username</db.user> <db.pwd>userpwd</db.pwd> </properties> </profile> <profile> <id>test</id> <properties> <db.driver>oracle.jdbc.driver.OracleDriver</db.driver> <db.url>jdbc:oracle:thin:@10.252.48.3:1521:testdbname</db.url> <db.user>testusername</db.user> <db.pwd>testuserpwd</db.pwd> </properties> </profile> </profiles>
在構建時可使用-P參數激活一個或多個profile,多個之間用逗號分隔
如 mvn clean install -Pdev
上面例子應該能夠看出profile是作什麼的,其實就至關於定義了一系列的profile變量,在具體構建時可用使用其中的某個profile去變量替換資源文件。
激活profile的方式有不少,如命令行激活(上面),settings文件顯式激活、系統屬性激活、操做系統環境激活、默認激活、文件存在與否激活等,具體能夠參考官網資料。
根據須要,能夠在如下文件聲明profile。
一、pom.xml 針對當前項目
二、用戶 settings.xml 用戶目錄下的.m2/settings.xml, 對當前用戶的全部項目有效。
三、全局 settings.xml 即maven安裝目錄下的conf/settings.xml。對本機上的全部項目有效。
在maven的web項目裏面,除了上面所說的資源文件(src/main/resources)以外,還有一類叫作web資源目錄,即src/main/webapp下面的js、css等等。默認狀況下,這些目錄是不被資源過濾的,開啓的命令以下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <webResources> <resource> <directory>src/main/webapp</directory> <filtering>true</filtering> <includes> <include>**/*.css</include> <include>**/*.js</include> </includes> </resource> </webResources> </configuration> </plugin>