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

uqxowvwt  于 2022-12-03  发布在  Maven
关注(0)|答案(9)|浏览(582)

我有一个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配置指出的那样,我该如何正确地配置它呢?

kgsdhlau

kgsdhlau1#

您的XML稍有错误,您需要在excludes父字段中添加任何类排除,因此根据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>

使用标准通配符语法,exclude字段的值应该是已编译类相对于目录目标/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

将显示已排除的类
无排除项

[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

希望这对你有帮助

pieyvz9o

pieyvz9o2#

虽然Andrew已经详细回答了这个问题,但我给出了如何在pom中排除它的代码

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <configuration>
                    <excludes>
                        <exclude>**/*com/test/vaquar/khan/HealthChecker.class</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <!-- prepare agent for measuring integration tests -->
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

适用于Springboot应用程序

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                       <!-- Exclude class from test coverage -->
                        <exclude>**/*com/khan/vaquar/Application.class</exclude>
                        <!-- Exclude full package from test coverage -->
                        <exclude>**/*com/khan/vaquar/config/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
x0fgdtte

x0fgdtte3#

另一种解决方案:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>default-check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <excludes>
                            <exclude>com.mypackage1</exclude
                            <exclude>com.mypackage2</exclude>
                        </excludes>
                        <element>PACKAGE</element>
                        <limits>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>COMPLEXITY</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.85</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

请注意,我们在配置中使用了"<element>PACKAGE</element>",这有助于我们在包级别排除。

hgqdbh6s

hgqdbh6s4#

您可以在声纳属性中配置覆盖范围排除,在jacoco插件的配置之外:

...
<properties>
    ....
    <sonar.exclusions>
        **/generated/**/*,
        **/model/**/*
    </sonar.exclusions>
    <sonar.test.exclusions>
        src/test/**/*
    </sonar.test.exclusions>
    ....
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
    <sonar.coverage.exclusions>
        **/generated/**/*,
        **/model/**/*
    </sonar.coverage.exclusions>
    <jacoco.version>0.7.5.201505241946</jacoco.version>
    ....
</properties>
....

并记住从插件中删除排除设置

2nc8po8w

2nc8po8w5#

使用声纳,覆盖范围,排除属性.

mvn clean install -Dsonar.coverage.exclusions=**/*ToBeExcluded.java

这将从保险额计算中排除这些类。

hiz5n14c

hiz5n14c6#

SonarQube文档中的一些示例:

xqk2d5yq

xqk2d5yq7#

https://github.com/jacoco/jacoco/issues/34
下面是我们对类的不同表示法:

  • 虚拟机名称:java/util/Map$Entry
  • Java名称:java.util.Map$项目档案
  • 名称:java/util/Map$Entry.class
    代理参数、Ant任务和Maven准备代理目标
  • 包括:Java名称(VM名称也适用)
  • 排除:Java名称(VM名称也可以)
  • exclclclassloader:Java名称

这些规范允许使用通配符 * 和?,其中 * 通配符可以使用任意数量的字符,甚至可以使用多个嵌套文件夹。

Maven报告目标

  • 包括:文件名
  • 排除:文件名

这些规范允许Ant Filespec使用通配符 *、**和?,其中 * 通配符仅表示单个路径元素的一部分。

vngu2lb8

vngu2lb88#

Sping Boot 2.5.3和Jacoco 0.8.4的出色表现^_^

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.4</version>
                <configuration>
                    <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
                    <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
                    <output>file</output>
                    <append>true</append>
                    <excludes>
                        <exclude>**/*com/example/Application.class</exclude>
                        <exclude>**/*com/example/modal*/**</exclude>
                        <exclude>**/*com/example/dto*/**</exclude>
                        <exclude>**/*com/example/mapper*/**</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
xuo3flqw

xuo3flqw9#

下面是pom.xml文件中的工作示例。

<plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>

        <executions>
            <execution>
                <id>prepare-agent</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                    <goal>report</goal>
                </goals>

            </execution>

            <execution>
                <id>default-check</id>
                <goals>
                    <goal>check</goal>
                </goals>

            </execution>
        </executions>
        <configuration>
            <dataFile>target/jacoco.exec</dataFile>
            <!-- Sets the output directory for the code coverage report. -->
            <outputDirectory>target/jacoco-ut</outputDirectory>
            <rules>
                <rule implementation="org.jacoco.maven.RuleConfiguration">
                    <element>PACKAGE</element>
                    <limits>
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>COMPLEXITY</counter>
                            <value>COVEREDRATIO</value>
                            <minimum>0.00</minimum>
                        </limit>
                    </limits>
                </rule>
            </rules>
            <excludes>
                <exclude>com/pfj/fleet/dao/model/**/*</exclude>
            </excludes>
            <systemPropertyVariables>

                <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
            </systemPropertyVariables>
        </configuration>
    </plugin>

相关问题