设为首页 收藏本站
查看: 929|回复: 0

[经验分享] maven常用插件配置和使用

[复制链接]

尚未签到

发表于 2017-2-28 11:04:11 | 显示全部楼层 |阅读模式
  本文主要是介绍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

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348356-1-1.html 上篇帖子: 自学JAVA要的项目中常用jar包 从网上下下来的一个包含大部分自学JAVA要的项目中常用jar包,亲们,应该有你要的吧!!(*^__^*) 嘻嘻…… 下篇帖子: 安卓Android大量项目源码实例喜欢的朋友都拿去吧
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表