Jacoco覆盖率工具使用之maven篇

出处:http://blog.csdn.net/wangmuming/article/details/28868833

Jacoco覆盖率工具使用之maven篇

 

说明

之前的文章已经介绍过如何使用apacheant 执行jacoco工具,下面开始介绍如何使用maven使用jacoco工具。

 

1.首先新建一个maven项目

      如图所示:
      

 

2:HelloWorld

    新建一个测试类helloworld,code 如图所示:

  

   

3:HelloWorldTest

  新建一个测试类helloworld test,code 如图所示:


4:编辑pom.xml文件

            编辑pom.xml文件,增加依赖包和jacoco配置,文件如下所示:
        
[java]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.test.jacoco</groupId>  
  4.   <artifactId>testJacoco</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.     
  7.   <name>JaCoCo Examples</name>  
  8.   
  9.   <properties>  
  10.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  11.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  12.   
  13.     <!-- Sonar -->  
  14.     <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>  
  15.     <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>  
  16.     <!-- The destination file for the code coverage report has to be set to the same value  
  17.          in the parent pom and in each module pom. Then JaCoCo will add up information in  
  18.          the same report, so that, it will give the cross-module code coverage. -->  
  19.     <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>  
  20.     <sonar.language>java</sonar.language>  
  21.   </properties>  
  22.   
  23.   <dependencies>  
  24.   <!--   
  25.     <dependency>  
  26.       <groupId>junit</groupId>  
  27.       <artifactId>junit</artifactId>  
  28.       <version>4.8.1</version>  
  29.       <scope>test</scope>  
  30.     </dependency>  
  31.      -->  
  32.     <dependency>  
  33.   <groupId>junit</groupId>  
  34.   <artifactId>junit</artifactId>  
  35.   <version>4.11</version>  
  36.   <scope>test</scope>  
  37. </dependency>  
  38.   </dependencies>  
  39.   
  40.   <build>  
  41.     <pluginManagement>  
  42.       <plugins>  
  43.         <plugin>  
  44.           <groupId>org.jacoco</groupId>  
  45.           <artifactId>jacoco-maven-plugin</artifactId>  
  46.           <version>0.5.3.201107060350</version>  
  47.         </plugin>  
  48.       </plugins>  
  49.     </pluginManagement>  
  50.   
  51.     <plugins>  
  52.       <plugin>  
  53.         <groupId>org.jacoco</groupId>  
  54.         <artifactId>jacoco-maven-plugin</artifactId>  
  55.         <configuration>  
  56.           <includes>com.*</includes>  
  57.         </configuration>  
  58.         <executions>  
  59.           <execution>  
  60.             <id>pre-test</id>  
  61.             <goals>  
  62.               <goal>prepare-agent</goal>  
  63.             </goals>  
  64.           </execution>  
  65.           <execution>  
  66.             <id>post-test</id>  
  67.             <phase>test</phase>  
  68.             <goals>  
  69.               <goal>report</goal>  
  70.             </goals>  
  71.           </execution>  
  72.         </executions>  
  73.       </plugin>  
  74.       <plugin>  
  75.         <groupId>org.apache.maven.plugins</groupId>  
  76.         <artifactId>maven-compiler-plugin</artifactId>  
  77.         <configuration>  
  78.           <source>1.5</source>  
  79.           <target>1.5</target>  
  80.         </configuration>  
  81.        </plugin>  
  82.     </plugins>  
  83.   </build>  
  84.    
  85. </project>  

5:打包测试

    如图所示:

    

6: 执行结果

   执行结果:




至此,基于maven的jacoco的使用讲解完了,整合jenkins 和 sonar 请参考“Jacoco覆盖率工具使用”;

微笑
`Maven` 是 Java 开发中的项目管理工具,而 `JaCoCo` 是一个流行的代码覆盖率工具,用于测量 Java 应用程序的单元测试代码覆盖情况。当使用 MavenJaCoCo 进行增量代码覆盖率,你可以设置 JaCoCo 以跳过对已知没有变化的文件(如空白或注释)的覆盖率计算,从而提高报告生成的速度。 要配置 Maven 使用 JaCoCo 进行增量代码覆盖率并忽略空格,你需要在你的 `pom.xml` 文件中添加以下 JaCoCo 相关的配置: ```xml <build> <plugins> <!-- Maven JaCoCo plugin --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> <!-- or your preferred version --> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> <!-- Configure the JaCoCo settings for code coverage analysis --> <configuration> <excludes> <!-- Add a pattern to exclude files with all whitespace characters --> <exclude>*</exclude> <exclude>**/generated/**</exclude> <!-- Exclude generated source directories if any --> </excludes> </configuration> </plugin> </plugins> </build> ``` 在这个配置中,`<excludes>` 标签定义了要排除的文件模式。`*` 匹配所有文件,然后你可以根据实际情况添加特定路径,比如 `**/generated/**` 来排除由编译器自动生成的代码。 然后,在运行 `mvn clean install` 或者包含 `verify` 目标JaCoCo 将只计算已修改的源文件的覆盖率,忽略了空白字符和其他未被覆盖的部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值