本文主要是介绍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中添加配置如下
<reporting>
<outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
b、运行goal
到项目根目录下运行mvn cobertura:cobertura 将会插桩class文件、测试、生成覆盖率报告
cobertura支持的goal如下
cobertura:check Check the Last Instrumentation Results.
cobertura:clean Clean up rogue files that cobertura maven plugin is tracking.
cobertura:dump-datafile Cobertura Datafile Dump Mojo.
cobertura:instrument Instrument the compiled classes.
cobertura:cobertura Instruments, Tests, and Generates a Cobertura Report.
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的配置
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</reporting>
b、运行goal
到项目根目录下运行mvn findbugs:findbugs将会开始检查,并生成bugs报告
findbugs支持的goal如下
findbugs:check
Fail the build if there were any FindBugs violations in the source code. An
XML report is put out by default in the target directory with the errors. To
see more documentation about FindBugs' options, please see the FindBugs
Manual..
findbugs:findbugs
Generates a FindBugs Report when the site plugin is run. The HTML report is
generated for site commands only.
findbugs:gui
Launch the Findbugs GUI. It will use all the parameters in the POM fle.
findbugs:help
Display help information on findbugs-maven-plugin.
Call
mvn findbugs:help -Ddetail=true -Dgoal=<goal-name>
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 插件介绍
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
直接运行mvn clean install会在target下打出两个包,带***-sources.jar的为源码包
2.5 maven的assembly插件
2.5.1 assembly插件可用来将工程依赖的jar包和工程都打成一个jar打包
2.5.2 maven2的 assembly 插件pom配置如下
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
直接运行mvn assembly:assembly会在target下出现***-with-dependencies.jar的jar包
2.6 插件开发
maven的插件开发相当简单,可以参考http://trinea.iteye.com/blog/1171957
已有 0 人发表留言,猛击->>这里 <<-参与讨论
ITeye推荐
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com