Maven Jacoco 配置 - 从报告中排除类/包不起作用 [英] Maven Jacoco Configuration - Exclude classes/packages from report not working

查看:94
本文介绍了Maven Jacoco 配置 - 从报告中排除类/包不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.7.2.201409121644</version><配置><outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory><exclude>some.package.*</exclude><exclude>**/*Config.*</exclude><exclude>**/*Dev.*</exclude><exclude>some/package/SomeClass.java</exclude></配置><执行><执行><目标><目标>准备代理</目标></目标></执行><执行><id>报告</id><phase>prepare-package</phase><目标><目标>报告</目标></目标></执行><执行><id>单元测试后</id><阶段>测试</阶段><目标><目标>报告</目标></目标></执行></执行></插件>

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

解决方案

您的 XML 有点错误,您需要在 excludes 父字段中添加任何类排除项,因此您的上述配置应按照 Jacoco 文档

<预><代码><配置><排除><exclude>**/*Config.*</exclude><exclude>**/*Dev.*</exclude></排除></配置>

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

* 匹配零个或多个字符** 匹配零个或多个目录?匹配单个字符

您也可以通过这种方式排除一个包及其所有子包/子包:

some/package/**/*;

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

我已经测试过并且我的报告目标报告了使用上述方法减少的类数量.

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

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

Sonar Docs 对此做了更多解释

在上面运行你的命令

mvn clean verify

将显示已排除的类

没有排除

[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @** ---[信息] 分析了 37 个类的包**"

有排除项

[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @** ---[信息] 分析了 34 个类的包**"

希望能帮到你

I have a maven multi-module project and I'm using jacoco-maven for code coverage reports. Some classes should not be reported, as they're Spring configuration and I'm not interested in them.

I have declared the maven-jacoco plugin as follow:

<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>

The problem is that when I execute mvn clean verify jacoco still reports classes that should have been excluded as my xml configuration points out. How can I configure it properly?

解决方案

Your XML is slightly wrong, you need to add any class exclusions within an excludes parent field, so your above configuration should look like the following as per the Jacoco docs

<configuration>
    <excludes>
        <exclude>**/*Config.*</exclude>
        <exclude>**/*Dev.*</exclude>
    </excludes>
</configuration>

The values of the exclude fields should be class paths (not package names) of the compiled classes relative to the directory target/classes/ using the standard wildcard syntax

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

You may also exclude a package and all of its children/subpackages this way:

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

This will exclude every class in some.package, as well as any children. For example, some.package.child wouldn't be included in the reports either.

I have tested and my report goal reports on a reduced number of classes using the above.

If you are then pushing this report into Sonar, you will then need to tell Sonar to exclude these classes in the display which can be done in the Sonar settings

Settings > General Settings > Exclusions > Code Coverage

Sonar Docs explains it a bit more

Running your command above

mvn clean verify

Will show the classes have been excluded

No exclusions

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

With exclusions

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

Hope this helps

这篇关于Maven Jacoco 配置 - 从报告中排除类/包不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆