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
  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覆盖率工具使用”;

说明

之前的文章已经介绍过如何使用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
  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覆盖率工具使用”;

猜你喜欢

转载自blog.csdn.net/hephec/article/details/79277710
今日推荐