4

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>
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • Possible duplicate of [Maven Jacoco Configuration - Exclude classes/packages from report not working](http://stackoverflow.com/questions/27799419/maven-jacoco-configuration-exclude-classes-packages-from-report-not-working) – Tunaki Feb 25 '16 at 17:16
  • 3
    You aren't setting the configuration for the right plugin. The exclusion should be for `jacoco-maven-plugin` but you don't have any. – Tunaki Feb 25 '16 at 17:17

2 Answers2

9

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

Jimit Patel
  • 4,265
  • 2
  • 34
  • 58
Stuti Verma
  • 1,059
  • 13
  • 32
  • Probably best not to configure the `prepare-agent` goal. From the docs: Except for performance optimization or technical corner cases this option is normally not required. If you want to exclude classes from the report please configure the `report` goal accordingly. – ordonezalex Jun 18 '21 at 01:54
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.

Eric Tan
  • 1,377
  • 15
  • 14