jacoco的maven插件使用

本文对比了Emma与JaCoCo两款Java代码覆盖率工具,并详细介绍了如何使用JaCoCo及其Maven插件进行代码覆盖率统计的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近要把java中的代码覆盖率搞起来,开始使用的是emma,百度了好久没有搞定,然后换成jacoco。其中收集了一些资料:

eclipse的eclemma插件,使用的jacoco, 搞起来最轻松

http://www.eclemma.org/ 


emma的maven插件:

http://www.cnblogs.com/morebetter/archive/2013/06/26/3156756.html


jacoco的maven插件:

http://blog.youkuaiyun.com/wangmuming/article/details/28868833

http://www.cnblogs.com/D-Key/p/4530876.html  ----想迅速用起来,这篇blog足够


emma好久不更新了,在配置的过程中,总是遇到:   Illegal local variable table start_pc 6 in method , 貌似是jdk版本高了。。。 然后放弃了。

改用jacoco,配置如下

pom.xml中添加:摘自http://www.cnblogs.com/D-Key/p/4530876.html

[html]  view plain  copy
  1. <span style="font-size:18px;"><build>  
  2.         <plugins>  
  3.             <!-- jacoco plugin -->  
  4.             <plugin>  
  5.                 <groupId>org.jacoco</groupId>  
  6.                 <artifactId>jacoco-maven-plugin</artifactId>  
  7.                 <version>0.7.9</version>  
  8.                 <executions>  
  9.                     <execution>  
  10.                         <!-- 在maven的initialize阶段,将Jacoco的runtime agent作为VM的一个参数 传给被测程序,用于监控JVM中的调用。 -->  
  11.                         <id>default-prepare-agent</id>  
  12.                         <goals>  
  13.                             <goal>prepare-agent</goal>  
  14.                         </goals>  
  15.   
  16.   
  17.                         <configuration>  
  18.                             <destFile>  
  19.                                 ${project.build.directory}/coverage-reports/jacoco.exec  
  20.                             </destFile>  
  21.                             <propertyName>surefireArgLine</propertyName>  
  22.                         </configuration>  
  23.   
  24.   
  25.                     </execution>  
  26.   
  27.   
  28.                     <!-- 在程序的verify阶段,执行report测试的程序。 文件的输入为perpare-agent阶段中设置或者默认的jacoco.exec.   
  29.                         参数 includes和excludes可用来选定report中过滤的类。 -->  
  30.                     <execution>  
  31.                         <id>default-report</id>  
  32.                         <phase>test</phase>  
  33.                         <goals>  
  34.                             <goal>report</goal>  
  35.                         </goals>  
  36.   
  37.   
  38.                         <configuration>  
  39.                             <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>  
  40.                             <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>  
  41.                         </configuration>  
  42.   
  43.   
  44.                     </execution>  
  45.   
  46.   
  47.                 </executions>  
  48.             </plugin>  
  49.   
  50.   
  51.             <!-- 使用 maven-surefire-plugin来执行单元测试。 将surefireArgLine赋值给argLine参数,以保证在测试执行时Jacoco   
  52.                 agent处于运行状态。 -->  
  53.             <plugin>  
  54.                 <groupId>org.apache.maven.plugins</groupId>  
  55.                 <artifactId>maven-surefire-plugin</artifactId>  
  56.                 <version>2.19.1</version>  
  57.                 <configuration>  
  58.                     <argLine>${surefireArgLine}</argLine>  
  59.                 </configuration>  
  60.             </plugin>  
  61.         </plugins>  
  62.     </build></span>  

mvn test,然后在target/site/.........找到统计结果
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值