Jacoco 检查与 jacoco 报告中的不同覆盖率值

Different coverage ratio values in Jacoco check vs jacoco report

我在我的项目中设置了 Jacoco 来执行 a) 检查代码覆盖率和 b) 生成覆盖率报告。生成的 HTML 报告显示了正确的覆盖率值,但 Jacoco 检查在构建过程结束时生成的警告消息显示了不同的错误值。我想知道我在这里做错了什么。

例如,这是我在生成的 HTML 报告中看到的实用程序 class 的内容:

但是对于相同的 class 我在构建完成时看到了这个:

这是我的 pom.xml 文件中的 jacoco-report 配置:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
        <execution>
            <id>jacoco.check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <phase>${jacoco.check.phase}</phase>
            <configuration>
                <skip>${jacoco.skip}</skip>
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>CLASS</element>
                        <excludes>
                            <exclude>com/xxxxxx/**/config/**/*</exclude>
                            <exclude>com/xxxxxx/**/filter/**/*</exclude>
                            <exclude>com/xxxxxx/xxxxxx/errors/*</exclude>
                            <exclude>com/xxxxxx/xxxxxx/security/*</exclude>
                            <exclude>com/xxxxxx/xxxxxx/xxxxxxOnlineWebApplication.class</exclude>
                        </excludes>
                        <limits>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.90</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
        <execution>
            <id>prepare-unit-tests</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>

        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <propertyName>itCoverageAgent</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我的 jacoco-check 配置如下:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
        <execution>
            <id>agent-for-ut</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.reportPaths}</destFile>
            </configuration>
        </execution>
        <!-- Integration-tests executed with failsafe-plugin -->
        <execution>
            <id>agent-for-it</id>
            <phase>package</phase>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.itReportPath}</destFile>
            </configuration>
        </execution>
    </executions>
    <configuration>
            <excludes>
                <exclude>com/xxxxxx/**/config/**/*</exclude>
                <exclude>com/xxxxxx/**/filter/**/*</exclude>
                <exclude>com/xxxxxx/xxxxxx/errors/*</exclude>
                <exclude>com/xxxxxx/xxxxxx/security/*</exclude>
                <exclude>com/xxxxxx/xxxxxx/xxxxxxOnlineWebApplication.class</exclude>
            </excludes>
    </configuration>
</plugin>

您似乎正在更改存储结果的位置:

<destFile>${sonar.jacoco.itReportPath}</destFile>

您还需要使用规则配置的 <dataFile> 项告诉检查插件在哪里可以找到数据文件。

请参阅jacoco:check docs了解更多信息。