【问题标题】:How to exclude a class from Jacoco coverage?如何从 Jacoco 覆盖范围中排除一个类?
【发布时间】:2016-06-08 14:23:39
【问题描述】:

我想以某种方式使用 Jacoco,以便将 Sample.java class 从整体覆盖范围中排除。为了实现这一点,我在 maven pom.xml 中的 prepare-agent 目标中包含了 <exclude>

Jacoco 插件:

                <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.1.201405082137</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

Surefire 插件:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
            <configuration>
                <excludes>
                    <exclude>**/*Sample.java</exclude>
                </excludes>
            </configuration>
        </plugin>

属性部分:

    <properties>
    <argLine>-Dfile.encoding=ISO-8859-1</argLine>
</properties>

【问题讨论】:

标签: maven jenkins jacoco cobertura jacoco-maven-plugin


【解决方案1】:

这是为 JaCoCo 配置排除/包含的正确方法:

    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.1.201405082137</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>**/*Sample.class</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
        </plugin>
    </plugins>

有关更多详细信息,您可以浏览此文档: http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html

【讨论】:

  • 应该是 Sample.class 而不是 Sample.java **/*Sample.class
  • 最好不要配置prepare-agent 目标。来自文档:除了性能优化或技术极端情况外,通常不需要此选项。如果您想从报告中排除课程,请相应地配置report 目标。
【解决方案2】:

这是我的解决方案,请注意排除类模式是class.path.className

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
            <haltOnFailure>true</haltOnFailure>
            <rules>
                <rule>
                    <element>CLASS</element>
                    <excludes>
                        <exclude>com.example.className</exclude>
                        <exclude>com.example.config.*</exclude>
                    </excludes>
                    <limits>
                        <limit>
                            <counter>LINE</counter>
                            <value>COVEREDRATIO</value>
                            <minimum>0.80</minimum>
                        </limit>
                    </limits>
                </rule>
            </rules>
            </configuration>
         </execution>
     </executions>
 </plugin>

配置详情请查看this doco,希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 2016-03-29
    • 2018-07-27
    • 2021-07-10
    • 1970-01-01
    • 2016-03-21
    • 2019-05-21
    • 2016-02-25
    • 2014-10-24
    相关资源
    最近更新 更多