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

问题描述

我有一个maven多模块项目,我使用jacoco-maven代码覆盖率报告。



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

p>

 < 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>
< / config>
< executions>
< execution>
< goals>
< goal> prepare-agent< / goal>
< / goal>
< / execution>
< execution>
< id> report< / id>
< phase> prepare-package< / phase>
< goals>
< goal> report< / goal>
< / goal>
< / execution>
< execution>
< id> post-unit-test< / id>
< phase> test< / phase>
< goals>
< goal> report< / goal>
< / goal>
< / execution>
< / executions>
< / plugin>

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

解决方案

您的XML有点错误,您需要在排除父字段中添加任何类排除,因此您的上述配置应类似于

 < configuration> 
< excludes>
< exclude> ** / * Config。*< / exclude>
< exclude> ** / * Dev。*< / exclude>
< / excludes>
< / config>

exclude字段的值应为编译类的类路径目录/ classes /使用标准通配符语法

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

我已经测试,我的报告目标报告减少。



如果你将这个报告推送到Sonar,你需要告诉Sonar在显示屏中排除这些类,这可以在Sonar设置中进行。 p>

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



解释一下



>

  mvn clean verify 

将显示已排除的类别



无排除

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



排除

  [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report(post-test)@ ** --- 
[INFO] 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

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配置 - 从报表中排除类/包不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:51