maven項目使用jacoco插件檢測代碼覆蓋率詳細配置


使用maven構建項目(java項目或者web項目都可以)

jacoco插件的配置參考官方網址:http://www.eclemma.org/jacoco/trunk/doc/maven.html

(1)配置jacoco的依賴jar包

<dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
</dependency>

 

(2)配置jacoco的插件,以及相關的goal

 <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.8</version>
    <!--這里的execution ,每一個執行的goal,對應的id必須是唯一的--> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution>
        <!--這個check:對代碼進行檢測,控制項目構建成功還是失敗--> <execution> <id>check</id> <goals> <goal>check</goal> </goals> </execution>
        <!--這個report:對代碼進行檢測,然后生成index.html在 target/site/index.html中可以查看檢測的詳細結果--> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>

 

(3)配置代碼檢查的約束 rules

<!-- Configuration 里面寫配置信息 -->
 <configuration>
     <!-- rules里面指定覆蓋規則 -->
     <rules>
       <rule implementation="org.jacoco.maven.RuleConfiguration">
         <element>BUNDLE</element>
         <limits>
          <!-- 指定方法覆蓋到80% -->
           <limit implementation="org.jacoco.report.check.Limit">
              <counter>METHOD</counter>
              <value>COVEREDRATIO</value>
              <minimum>0.80</minimum>
           </limit>
           <!-- 指定指令覆蓋到80% -->
           <limit implementation="org.jacoco.report.check.Limit">
              <counter>INSTRUCTION</counter>
              <value>COVEREDRATIO</value>
              <minimum>0.80</minimum>
           </limit>
           <!-- 指定行覆蓋到80% -->
           <limit implementation="org.jacoco.report.check.Limit">
             <counter>LINE</counter>
             <value>COVEREDRATIO</value>
             <minimum>0.80</minimum>
           </limit>
           <!-- 指定類覆蓋到100%,不能遺失任何類 -->
           <limit implementation="org.jacoco.report.check.Limit">
             <counter>CLASS</counter>
             <value>MISSEDCOUNT</value>
             <maximum>0</maximum>
           </limit>
         </limits>
      </rule>
   </rules>
</configuration>

 

(4)完整的pom.xml的配置如下

pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>

    <groupId>cn.demo</groupId>
    <artifactId>answers</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>answers</name>
    <url>http://maven.apache.org</url>
  
    <build>
        <finalName>answers</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

        <!--檢查代碼覆蓋率的插件配置--> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.8</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>check</id> <goals> <goal>check</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> <!-- Configuration 里面寫配置信息 --> <configuration> <!-- rules里面指定覆蓋規則 --> <rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <!-- 指定方法覆蓋到80% --> <limit implementation="org.jacoco.report.check.Limit"> <counter>METHOD</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <!-- 指定指令覆蓋到80% --> <limit implementation="org.jacoco.report.check.Limit"> <counter>INSTRUCTION</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <!-- 指定行覆蓋到80% --> <limit implementation="org.jacoco.report.check.Limit"> <counter>LINE</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <!-- 指定類覆蓋到100%,不能遺失任何類 --> <limit implementation="org.jacoco.report.check.Limit"> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules> </configuration> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.8</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM