qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          maven常用插件配置和使用

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

           

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

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

           

          1 自帶插件

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

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

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

           

          2 第三方插件

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

          如jetty 對于web開發(fā)使用jetty作為容器

          native 編譯c和c++代碼

          sql 執(zhí)行sql腳本

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

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

           

          2.2 maven2的cobertura插件

          2.2.1 cobertura是一款用來計算java代碼測試覆蓋率的工具,基于jcoverage。能計算每個類、包、整個工程的行覆蓋率和分支覆蓋率以及代碼復(fù)雜度(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、運行g(shù)oal

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

          cobertura支持的goal如下

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

          mvn cobertura:cobertura執(zhí)行前會執(zhí)行test phase,即執(zhí)行單側(cè)代碼

           

          2.3 maven2的findbugs插件

          2.3.1 findbugs是靜態(tài)檢查java代碼的工具,根據(jù)一些bugs的表達式檢查代碼中的bugs,可以自定義檢查規(guī)則

           

          2.3.2 maven2的findbugs插件介紹

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

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

          不同goal的配置略有不同,可自己調(diào)整,以下介紹的是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、運行g(shù)oal

          到項目根目錄下運行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下出現(xiàn)***-with-dependencies.jar的jar包

           

          2.6 插件開發(fā)

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

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

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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南皮县| 布尔津县| 瓦房店市| 南召县| 乳山市| 太谷县| 大新县| 五台县| 永新县| 义马市| 秦皇岛市| 张家口市| 嫩江县| 晋城| 东丰县| 蒙城县| 深州市| 白山市| 康保县| 平塘县| 诏安县| 皮山县| 左贡县| 卢湾区| 噶尔县| 义乌市| 苍溪县| 建德市| 宜章县| 盘山县| 南康市| 黄龙县| 阜新| 鄄城县| 亚东县| 千阳县| 福泉市| 昌都县| 水城县| 德保县| 阳山县|