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 阅读(420) 评论(0)  编辑  收藏 所属分类: maven


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜