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覆蓋率工具使用”;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章