qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          maven常用插件配置和使用

          本文主要是介紹maven的幾個常見第三方插件(cobertura、findbugs、source、assembly、插件開發)配置和使用,接http://trinea.iteye.com/blog/1290898

           

          maven本質上是一個插件框架,它的所有工作都交給插件來做,每個插件可以有多個goal

          除了自帶的插件之外還有很多比較成熟的第三方插件,我們也很容易上手進行簡單的插件開發,下面一一介紹

           

          1 自帶插件

          maven自帶的核心插件為Build plugins和Reporting plugins。

          mvn compile編譯源碼實際上就利用到了maven-compiler-plugin,其他phase也類似用到了相應的插件

          關于maven自帶的核心插件見:http://maven.apache.org/plugins/index.html

           

          2 第三方插件

          2.1 maven有很多成熟的第三方插件

          如jetty 對于web開發使用jetty作為容器

          native 編譯c和c++代碼

          sql 執行sql腳本

          其他更多見:http://maven.apache.org/plugins/index.html#Outside_The_Maven_Land

          下面具體介紹下單元測試覆蓋率插件cobertura、findbugs

           

          2.2 maven2的cobertura插件

          2.2.1 cobertura是一款用來計算java代碼測試覆蓋率的工具,基于jcoverage。能計算每個類、包、整個工程的行覆蓋率和分支覆蓋率以及代碼復雜度(Cyclomatic complexity)并生成html或xml形式的報告,讓用戶很方便的查看代碼的單元測試覆蓋率情況。cobertura的原理是通過對class文件進行插樁然后計算。

           

          2.2.2 maven2的cobertura插件介紹

          插件地址為http://mojo.codehaus.org/cobertura-maven-plugin/index.html

          a、首先在pom中添加配置如下

          Xml代碼  收藏代碼
          1. <reporting>  
          2.     <outputDirectory>target/site</outputDirectory>  
          3.     <plugins>  
          4.         <plugin>  
          5.             <groupId>org.codehaus.mojo</groupId>  
          6.             <artifactId>cobertura-maven-plugin</artifactId>  
          7.         </plugin>  
          8.     </plugins>    
          9. </reporting>   

          b、運行goal

          到項目根目錄下運行mvn cobertura:cobertura 將會插樁class文件、測試、生成覆蓋率報告

          cobertura支持的goal如下

          c、在target\site\cobertura目錄下生成報告文件,打開index.html可以查看具體報告

          mvn cobertura:cobertura執行前會執行test phase,即執行單側代碼

           

          2.3 maven2的findbugs插件

          2.3.1 findbugs是靜態檢查java代碼的工具,根據一些bugs的表達式檢查代碼中的bugs,可以自定義檢查規則

           

          2.3.2 maven2的findbugs插件介紹

          插件地址為http://mojo.codehaus.org/findbugs-maven-plugin/index.html

          a、首先在pom中添加配置如下

          不同goal的配置略有不同,可自己調整,以下介紹的是mvn findbugs:findbugs的配置

          Xml代碼  收藏代碼
          1. <reporting>  
          2.     <plugins>  
          3.       <plugin>  
          4.         <groupId>org.codehaus.mojo</groupId>  
          5.         <artifactId>findbugs-maven-plugin</artifactId>  
          6.         <version>2.3.1</version>  
          7.       </plugin>  
          8.     </plugins>  
          9. </reporting>  

          b、運行goal

          到項目根目錄下運行mvn findbugs:findbugs將會開始檢查,并生成bugs報告

          findbugs支持的goal如下

          Xml代碼  收藏代碼
          1. findbugs:check  
          2.   Fail the build if there were any FindBugs violations in the source code. An  
          3.   XML report is put out by default in the target directory with the errors. To  
          4.   see more documentation about FindBugs' options, please see the FindBugs  
          5.   Manual..  
          6.   
          7. findbugs:findbugs  
          8.   Generates a FindBugs Report when the site plugin is run. The HTML report is  
          9.   generated for site commands only.  
          10.   
          11. findbugs:gui  
          12.   Launch the Findbugs GUI. It will use all the parameters in the POM fle.  
          13.   
          14. findbugs:help  
          15.   Display help information on findbugs-maven-plugin.  
          16.   Call  
          17.     mvn findbugs:help -Ddetail=true -Dgoal=<goal-name>  
          18.   to display parameter details.  

          c、在target\site\findbugs目錄下生成報告文件,打開index.html可以查看具體報告

          mvn findbugs:findbugs綁定到了compile phase,即在編譯時自動檢查

          http://qa.taobao.com/?p=4206

           

          2.4 maven的source插件

          2.4.1 source插件用來將工程打包成帶源代碼的jar包

          2.4.2 maven2的source插件介紹

          Xml代碼  收藏代碼
          1. <build>  
          2.     <plugins>  
          3.       <plugin>  
          4.         <groupId>org.apache.maven.plugins</groupId>  
          5.         <artifactId>maven-source-plugin</artifactId>  
          6.         <version>2.1.2</version>  
          7.         <executions>  
          8.           <execution>  
          9.             <id>attach-sources</id>  
          10.             <phase>verify</phase>  
          11.             <goals>  
          12.               <goal>jar-no-fork</goal>  
          13.             </goals>  
          14.           </execution>  
          15.         </executions>  
          16.       </plugin>  
          17.     </plugins>  
          18. </build>  

          直接運行mvn clean install會在target下打出兩個包,帶***-sources.jar的為源碼包

           

          2.5 maven的assembly插件

          2.5.1 assembly插件可用來將工程依賴的jar包和工程都打成一個jar打包

          2.5.2 maven2的assembly插件pom配置如下

          Xml代碼  收藏代碼
          1. <build>  
          2.     <plugins>         
          3.       <plugin>  
          4.         <artifactId>maven-assembly-plugin</artifactId>  
          5.         <configuration>  
          6.           <descriptorRefs>  
          7.             <descriptorRef>jar-with-dependencies</descriptorRef>  
          8.           </descriptorRefs>  
          9.         </configuration>  
          10.       </plugin>  
          11.     </plugins>  
          12. </build>  

          直接運行mvn assembly:assembly會在target下出現***-with-dependencies.jar的jar包

           

          2.6 插件開發

          maven的插件開發相當簡單,可以參考http://trinea.iteye.com/blog/1171957

          posted on 2014-03-31 17:29 順其自然EVO 閱讀(457) 評論(0)  編輯  收藏 所屬分類: maven

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 赤峰市| 灵宝市| 武夷山市| 衡水市| 梁平县| 海门市| 汉沽区| 江陵县| 容城县| 宿松县| 岱山县| 进贤县| 孝昌县| 晴隆县| 万山特区| 万盛区| 佛山市| 息烽县| 登封市| 铁力市| 柏乡县| 宁蒗| 靖远县| 汾西县| 宁明县| 汝南县| 滦平县| 象州县| 磴口县| 姚安县| 密山市| 鹿邑县| 榆树市| 安陆市| 青海省| 色达县| 祁连县| 平罗县| 高尔夫| 卓资县| 常熟市|