gpt4 book ai didi

Maven Jacoco 配置 - 从报告中排除类/包不起作用

转载 作者:行者123 更新时间:2023-12-03 05:11:02 28 4
gpt4 key购买 nike

我有一个 Maven 多模块项目,我正在使用 jacoco-maven 来生成代码覆盖率报告。有些类不应该报告,因为它们是 Spring 配置,我对它们不感兴趣。

我已声明 maven-jacoco 插件如下:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<configuration>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
<exclude>some.package.*</exclude>
<exclude>**/*Config.*</exclude>
<exclude>**/*Dev.*</exclude>
<exclude>some/package/SomeClass.java</exclude>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

问题是,当我执行mvn clean verify时,jacoco仍然报告我的xml配置指出的应该被排除的类。如何正确配置?

最佳答案

您的 XML 略有错误,您需要在排除父字段中添加任何类排除项,因此您的上述配置应如下所示,按照 Jacoco docs

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<configuration>
<excludes>
<exclude>**/*Config.*</exclude>
<exclude>**/*Dev.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>jacoco-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

排除字段的值应该是使用标准通配符语法相对于目录 target/classes/的已编译类的类路径(而不是包名称)

*   Match zero or more characters
** Match zero or more directories
? Match a single character

您还可以通过这种方式排除包及其所有子包:

<exclude>some/package/**/*</exclude>

这将排除 some.package 中的每个类以及任何子类。例如,some.package.child 也不会包含在报告中。

我已经进行了测试,并且我的报告目标报告了使用上述内容减少的类数量。

如果您随后将此报告推送到 Sonar,则需要告诉 Sonar 在显示中排除这些类,这可以在 Sonar 设置中完成

设置 > 常规设置 > 排除 > 代码覆盖率

Sonar Docs稍微解释一下

运行上面的命令

mvn clean verify

将显示已排除的类(class)

没有排除

[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
[INFO] Analyzed bundle '**' with 37 classes

有排除

[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
[INFO] Analyzed bundle '**' with 34 classes

关于Maven Jacoco 配置 - 从报告中排除类/包不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799419/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com