如何从 Jacoco 覆盖范围中排除 class?

How to exclude a class from Jacoco coverage?

我想以某种方式使用 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>

这是为 JaCoCo 配置 excludes/includes 的正确方法:

    <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

这是我的解决方案,请注意排除 class 模式是 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,希望对您有所帮助。