qileilove

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

          maven常用插件配置和使用

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

           

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

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

           

          1 自帶插件

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

          mvn compile編譯源碼實(shí)際上就利用到了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是一款用來計(jì)算java代碼測試覆蓋率的工具,基于jcoverage。能計(jì)算每個(gè)類、包、整個(gè)工程的行覆蓋率和分支覆蓋率以及代碼復(fù)雜度(Cyclomatic complexity)并生成html或xml形式的報(bào)告,讓用戶很方便的查看代碼的單元測試覆蓋率情況。cobertura的原理是通過對class文件進(jìn)行插樁然后計(jì)算。

           

          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、運(yùn)行g(shù)oal

          到項(xiàng)目根目錄下運(yùn)行mvn cobertura:cobertura 將會(huì)插樁class文件、測試、生成覆蓋率報(bào)告

          cobertura支持的goal如下

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

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

           

          2.3 maven2的findbugs插件

          2.3.1 findbugs是靜態(tài)檢查java代碼的工具,根據(jù)一些bugs的表達(dá)式檢查代碼中的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、運(yùn)行g(shù)oal

          到項(xiàng)目根目錄下運(yùn)行mvn findbugs:findbugs將會(huì)開始檢查,并生成bugs報(bào)告

          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目錄下生成報(bào)告文件,打開index.html可以查看具體報(bào)告

          mvn findbugs:findbugs綁定到了compile phase,即在編譯時(shí)自動(dòng)檢查

          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>  

          直接運(yùn)行mvn clean install會(huì)在target下打出兩個(gè)包,帶***-sources.jar的為源碼包

           

          2.5 maven的assembly插件

          2.5.1 assembly插件可用來將工程依賴的jar包和工程都打成一個(gè)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>  

          直接運(yùn)行mvn assembly:assembly會(huì)在target下出現(xiàn)***-with-dependencies.jar的jar包

           

          2.6 插件開發(fā)

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

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

          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 永兴县| 甘南县| 集贤县| 孙吴县| 靖州| 顺平县| 江永县| 册亨县| 大兴区| 张家口市| 溧水县| 普兰店市| 昂仁县| 山西省| 乐东| 库车县| 二手房| 阜新市| 巴林右旗| 额济纳旗| 三都| 辉县市| 赤峰市| 略阳县| 雷州市| 寻乌县| 集贤县| 垫江县| 阜南县| 温宿县| 高青县| 文山县| 大洼县| 广水市| 绥江县| 房山区| 平定县| 永丰县| 宾阳县| 武隆县| 博白县|