How to exclude a class from Jacoco coverage?

27,617

Solution 1

This is the right way to configure excludes/includes for 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>

For more details, you can go through this documentation: http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html

Solution 2

Here's my solution, please note the exclude class pattern is 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>

And for the config detail, please check this doco, hope it helps.

Share:
27,617

Related videos on Youtube

meallhour
Author by

meallhour

Updated on July 05, 2022

Comments

  • meallhour
    meallhour 10 months

    I want to use Jacoco in a way so that it excludes a Sample.java class from the overall coverage. To achieve that I have included <exclude> within prepare-agent goal in maven pom.xml

    Jacoco plugin:

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

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

        <properties>
        <argLine>-Dfile.encoding=ISO-8859-1</argLine>
    </properties>
    
    • Tunaki
      Tunaki about 7 years
      You aren't setting the configuration for the right plugin. The exclusion should be for jacoco-maven-plugin but you don't have any.

Related