深入理解maven及應用--轉


(一):生命周期和插件

原文地址:http://blog.csdn.net/chaofanwei/article/details/36197183

在項目里用了快一年的maven了,最近突然發現maven項目在eclipse中build時非常慢,因為經常用clean install命令來build項目,也沒有管那么多,但最近實在受不了烏龜一樣的build速度,於是下定決心再看看《maven實戰》吧,
        對於我來說,maven最主要的作用有兩個方面,一個是對jar包的依賴解決功能,自己管理jar包,另一個功能就是項目的構建,打包部署。現在我覺得最重要的還是maven的生命周期和插件機制,下面就來總結一下吧。

    參考url:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html


1、三套生命周期


     對於maven的生命周期來說,共有三個相互獨立的生命周期,分別是clean、default、site。clean生命周期目的是清理項目,default生命周期目的是構建項目,而site生命周期目的是建立項目站點。
     每個生命周期分別包含一些階段,這些階段是有順序的,並且后面的階段依賴於前面的階段。如clean生命周期包含pre-clean、clean和post-clean三個階段,如果執行clean階段,則會先執行pre-clean階段。
     較之於生命周期階段有前后依賴關系,三套生命周期本身是相互獨立的,用戶可以僅調用clean生命周期的某個階段,也可以不執行clean周期,而直接執行default生命周期的某幾個階段。


2、clean生命周期


     clean生命周期包含三個階段,主要負責清理項目,如下:

 

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

 


3、default生命周期


    default生命周期定義了真正構建時所需要執行的所有步驟,包含的階段如下:

 

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.

 

 

4、site生命周期


     siet生命周期的目的是建立和發布項目站點,maven能夠基於POM所包含的信息,自動生成一個友好的站點,方便團隊交流和發布項目信息,包含的階段如下:

    

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

 

5、命令行與生命周期

     從命令行執行maven任務的最主要方式就是調用maven的生命周期階段。需要注意的是,各個生命周期是相互獨立的,而一個生命周期的階段是有前后依賴關系的。例子如下:
     1、$mvn clean :該命令調用clean生命周期的clean階段。實際執行的階段為clean生命周期的pre-clean和clean階段。
     2、$mvn test:該命令調用default生命周期的test階段。實際調用的是default生命周期的validate、initialize等,直到test的所有階段。
     3、$mvn clean install:該命令調換用clean生命周期的clean階段和default生命周期的instal階段。

6、插件目標

      maven的核心僅僅定義了抽象的生命周期,具體的任務是交由插件完成的,插件以獨立的形式存在。
     對於插件本身,為了能夠復用代碼,它往往能夠完成多個任務。如maven-dependency-plugin有十多個目標,每個目標對應了一個功能,如 dependency:analyze、 dependency:tree和dependency:list。這是一種通用的寫法,冒號前面是插件前綴,后面是該插件的目標。

7、插件綁定

 maven的生命周期與插件相互綁定,用以完成實際的構建任務。具體而言,是生命周期的階段與插件的目標相互綁定,已完成某個具體的構建任務。例如項目編譯這一任務,它對應了default生命周期的compile階段,而maven-compiler-plugin這一插件的compile目標能夠完成該任務,因此將他們綁定。

7.1內置綁定

 maven在核心為一些主要的生命周期接到綁定了很多插件的目標,如下:
     clean和site生命周期相對簡單。

 

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

 

 

7、2自定義綁定

     除了內置綁定以為,用戶還能夠自己選擇獎某個插件目標綁定到生命周期的某個階段以執行更多更特色的任務。

[html]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. <!-- 自動復制資源文件件到根目錄 -->  
  2.             <plugin>  
  3.                 <groupId>org.apache.maven.plugins</groupId>  
  4.                 <artifactId>maven-resources-plugin</artifactId>  
  5.                 <version>2.6</version>  
  6.                 <configuration>  
  7.                     <includeEmptyDirs>true</includeEmptyDirs>  
  8.                     <encoding>GBK</encoding>  
  9.                     <nonFilteredFileExtensions>  
  10.                         <nonFilteredFileExtension>exe</nonFilteredFileExtension>  
  11.                         <nonFilteredFileExtension>zip</nonFilteredFileExtension>  
  12.                         <nonFilteredFileExtension>vbs</nonFilteredFileExtension>  
  13.                         <nonFilteredFileExtension>sh</nonFilteredFileExtension>  
  14.                     </nonFilteredFileExtensions>  
  15.                 </configuration>  
  16.                 <executions>  
  17.                     <execution>  
  18.                         <id>copy-resources</id>  
  19.                         <phase>validate</phase>  
  20.                         <goals>  
  21.                             <goal>copy-resources</goal>  
  22.                         </goals>  
  23.                         <configuration>  
  24.                             <includeEmptyDirs>true</includeEmptyDirs>  
  25.                             <outputDirectory>${project.build.directory}</outputDirectory>  
  26.                             <excludes>  
  27.                                 <exclude>agentmanager.jsmooth</exclude>  
  28.                                 <exclude>assembly.xml</exclude>  
  29.                             </excludes>  
  30.                             <resources>  
  31.                                 <resource>  
  32.                                     <directory>src/main/resources/</directory>  
  33.                                     <filtering>true</filtering>  
  34.                                 </resource>  
  35.                             </resources>  
  36.                         </configuration>  
  37.                     </execution>  
  38.                 </executions>  
  39.             </plugin>  


     如上圖定義了一個id為copy-resources的任務,綁定到default生命周期的validate階段,綁定的插件為maven-resources-plugin,插件目標為copy-resources。即用插件的copy-resources功能來實現項目資源文件的拷貝。

[html]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. <!-- 自動復制maven依賴包到lib目錄 -->  
  2.             <plugin>  
  3.                 <groupId>org.apache.maven.plugins</groupId>  
  4.                 <artifactId>maven-dependency-plugin</artifactId>  
  5.                 <version>2.1</version>  
  6.                 <executions>  
  7.                     <execution>  
  8.                         <id>copy</id>  
  9.                         <phase>install</phase>  
  10.                         <goals>  
  11.                             <goal>copy-dependencies</goal>  
  12.                         </goals>  
  13.                         <configuration>  
  14.                             <outputDirectory>lib</outputDirectory>  
  15.                         </configuration>  
  16.                     </execution>  
  17.                 </executions>  
  18.             </plugin>  


同上,定義了一個id為copy的任務,利用插件maven-dependency-plugin的copy-dependencies目標綁定到default生命周期的install階段,來實現項目依賴的jar包的自動復制。

     當插件目標被綁定到不同的生命周期階段時候,其執行順序會有生命周期階段的先后順序決定的。如果多個目標被綁定到同一個階段,他們的執行順序是由插件聲明的先后順序決定目標的執行順序

8、插件配置


 用戶可以配置插件目標的參數,進一步調整插件目標所執行的任務。

8、1命令行插件配置

 如 $mvn install -Dmaven.test.skip=true 的意義即跳過測試步驟。
      參數-D的java自帶的,其功能是通過命令行設置一個java系統屬性,maven簡單地重用了該參數以實現插件參數的配置。

8、2pom中插件全局配置

如項目編譯使用1.6版本的源文件,生成與JVM1.6兼容的字節碼文件,如下:

[html]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-compiler-plugin</artifactId>  
  4.                 <version>2.3.2</version>  
  5.                 <configuration>  
  6.                     <source>1.6</source>  
  7.                     <target>1.6</target>  
  8.                 </configuration>  
  9.             </plugin>  


 

9、獲取插件描述信息

    $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

(二):靈活的構建

原文地址:http://blog.csdn.net/chaofanwei/article/details/36652373

參考官方url:http://maven.apache.org/guides/index.html

 一個優秀的構建系統必須足夠靈活,應該能夠讓項目在不同的環境下都能成功構建。maven為了支持構建的靈活性,內置了三大特性,即:屬性、profile和資源過濾

1、maven屬性

 maven屬性分6類:
    1、內置屬性:如${basedir}表示項目根目錄,${version}表示項目版本
    2、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文件的任意節點的內容 
    3、自定義屬性:用戶可以在pom的<properties>元素下自定義maven屬性。
    4、setting屬性:用戶可以使用以settings開頭的屬性引用settings.xml中xml元素的值,如${settings.localRepository}指向用戶本地倉庫的地址。
    5、java系統屬性:maven可以使用當前java系統的屬性,如${user.home}指向了用戶目錄。
    6、環境變量屬性:所有環境變量都可以使用以env.開頭的屬性。如:${env.JAVA_HOE}。

2、資源過濾

     這里所謂的資源:也就就是指src/main/resources和src/test/resources文件下的所有文件,默認情況下,這些文件會被復制到classpath下面,即target/classes下面。
     所謂資源過濾,就是過濾這些文件夾下面的文件里面的內容,看里面的maven變量是否需要替換。默認情況下,只有pom.xml里面的變量才會被替換,資源文件是不會被過濾的,但是可以設置,如下:

[html]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. <build>  
  2.         <finalName>agentmanager</finalName>  
  3.         <sourceDirectory>src/main/java</sourceDirectory>  
  4.         <resources>  
  5.             <!-- 控制資源文件的拷貝 -->  
  6.             <resource>  
  7.                 <directory>src/main/resources</directory>  
  8.                 <excludes>  
  9.                     <exclude>**/jre.zip</exclude>  
  10.                     <exclude>**/jre.tar</exclude>  
  11.                     <exclude>agentmanager.jsmooth</exclude>  
  12.                     <exclude>assembly.xml</exclude>  
  13.                 </excludes>  
  14.                 <targetPath>${project.build.directory}</targetPath>  
  15.             </resource>  
  16.             <resource>  
  17.                 <directory>src/main/resources/conf</directory>  
  18.                 <targetPath>${basedir}/conf</targetPath>  
  19.                 <filtering>true</filtering>  
  20.             </resource>  
  21.         </resources>  
  22.     </build>  


 

如jdbc.properties

[css]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. jdbc.driverClassName=${db.driver}  
  2. jdbc.url=${db.url}  
  3. jdbc.username=${db.user}  
  4. jdbc.password=${db.pwd}  

 

profile文件

[html]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. <profiles>  
  2.  <profile>  
  3.   <id>dev</id>  
  4.   <properties>  
  5.    <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>  
  6.    <db.url>jdbc:oracle:thin:@10.252.48.3:1521:dbname</db.url>  
  7.    <db.user>username</db.user>  
  8.    <db.pwd>userpwd</db.pwd>  
  9.   </properties>  
  10.  </profile>  
  11.  <profile>  
  12.   <id>test</id>  
  13.   <properties>  
  14.    <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>  
  15.    <db.url>jdbc:oracle:thin:@10.252.48.3:1521:testdbname</db.url>  
  16.    <db.user>testusername</db.user>  
  17.    <db.pwd>testuserpwd</db.pwd>  
  18.   </properties>  
  19.  </profile>  
  20. </profiles>  



在構建時可以使用-P參數激活一個或多個profile,多個之間用逗號分隔
如 mvn clean install -Pdev

3、maven profile

     上面例子應該可以看出profile是做什么的,其實就相當於定義了一系列的profile變量,在具體構建時可用使用其中的某個profile去變量替換資源文件。
      激活profile的方式有很多,如命令行激活(上面),settings文件顯式激活、系統屬性激活、操作系統環境激活、默認激活、文件存在與否激活等,具體可以參考官網資料


3.1 profile的種類

     根據需要,可以在以下文件聲明profile。
      1、pom.xml 針對當前項目
      2、用戶 settings.xml 用戶目錄下的.m2/settings.xml, 對當前用戶的所有項目有效。
      3、全局 settings.xml 即maven安裝目錄下的conf/settings.xml。對本機上的所有項目有效。

4、web資源過濾

     在maven的web項目里面,除了上面所說的資源文件(src/main/resources)之外,還有一類叫做web資源目錄,即src/main/webapp下面的js、css等等。默認情況下,這些目錄是不被資源過濾的,開啟的命令如下:

[html]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
    1.   <plugin>  
    2.     <groupId>org.apache.maven.plugins</groupId>  
    3.     <artifactId>maven-war-plugin</artifactId>  
    4.     <version>2.1.1</version>  
    5.     <configuration>  
    6.         <webResources>  
    7.         <resource>  
    8.             <directory>src/main/webapp</directory>  
    9.             <filtering>true</filtering>  
    10.             <includes>  
    11.             <include>**/*.css</include>  
    12.             <include>**/*.js</include>  
    13.             </includes>                            
    14.         </resource>      
    15.         </webResources>  
    16.     </configuration>  
    17. </plugin>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM