How to exclude a class from Jacoco coverage?

meallhour picture meallhour · Feb 25, 2016 · Viewed 22.5k times · Source

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>

Answer

Stuti Verma picture Stuti Verma · Aug 11, 2017

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