Maven Jacoco configuration - exclude classes / packages from report not working

I have a multi-module maven project and I use jacoco-maven for code coverage reports. Some classes should not be reported since they are Spring, and I am not interested in them.

I declared the maven-jacoco plugin as follows:

<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.2.201409121644</version> <configuration> <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory> <exclude>some.package.*</exclude> <exclude>**/*Config.*</exclude> <exclude>**/*Dev.*</exclude> <exclude>some/package/SomeClass.java</exclude> </configuration> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> 

The problem is that when I run mvn clean verify , jacoco still reports the classes that should have been excluded, as my xml configuration indicates. How can I configure it correctly?

+68
spring maven configuration jacoco jacoco-maven-plugin
Jan 06 '15 at 13:11
source share
7 answers

Your XML code is a bit wrong, you need to add class exceptions to the parent exception exception field, so your above configuration should look like this: Jacoco docs

 <configuration> <excludes> <exclude>**/*Config.*</exclude> <exclude>**/*Dev.*</exclude> </excludes> </configuration> 

The values โ€‹โ€‹of exclude fields must be classes (not package names) of compiled classes relative to the target directory / classes / using standard wildcard syntax

 * Match zero or more characters ** Match zero or more directories ? Match a single character 

You can also exclude a package and all its child / subpackages as follows:

 <exclude>some/package/**/*</exclude> 

This excludes every class in some.package , as well as all children. For example, some.package.child also not be included in reports.

I tested and reported the results of the report on the reduced number of classes using the above.

If you then click this report on Sonar, you will need to tell Sonar to exclude these classes from the display, which can be done in the Sonar settings.

Settings> General Settings> Exceptions> Code Coverage

Sonar Docs explains it a bit more

Running your command above

 mvn clean verify 

Will show that classes have been excluded

With no exceptions

 [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** --- [INFO] Analyzed bundle '**' with 37 classes 

With exceptions

 [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** --- [INFO] Analyzed bundle '**' with 34 classes 

Hope this helps

+125
Mar 06 '15 at 10:33
source share
โ€” -

Although Andrei has already answered the question with the details, I give a code on how to exclude it in

  <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.9</version> <configuration> <excludes> <exclude>**/*com/test/vaquar/khan/HealthChecker.class</exclude> </excludes> </configuration> <executions> <!-- prepare agent for measuring integration tests --> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>jacoco-site</id> <phase>package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> 

For Springboot application

 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <configuration> <excludes> <!-- Exclude class from test coverage --> <exclude>**/*com/khan/vaquar/Application.class</exclude> <!-- Exclude full package from test coverage --> <exclude>**/*com/khan/vaquar/config/**</exclude> </excludes> </configuration> </plugin> </plugins> </build> 
+7
Jun 06 '17 at 19:20
source share

https://github.com/jacoco/jacoco/issues/34

These are different notations for classes:

  • Virtual Machine Name: java / util / Map $ Entry
  • Java name: java.util.Map $ record file
  • Name: java / util / Map $ Entry.class

Agent Settings, Ant Tasks, and Maven Agent Preparation Purpose

  • includes: Java name (also virtual machine name)
  • excludes: Java name (also virtual machine name)
  • exclclassloader: Java name

These specifications allow wildcards * and?, Where * are wildcards of any number of characters, even several subfolders.

Maven Report Purpose

  • includes: file name
  • excludes: file name

These specifications allow Ant Filespec as wildcards *, **, and ?, where * are wildcards for only one path element.

+3
Mar 15 '17 at 12:37
source share

Another solution:

 <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.5.201505241946</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <excludes> <exclude>com.mypackage1</exclude <exclude>com.mypackage2</exclude> </excludes> <element>PACKAGE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.85</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin> 

Note that we use "<element>PACKAGE</element>" in the configuration, which then helps us exclude at the package level.

+3
Jan 02 '18 at 9:02
source share

Use the sonar.coverage.exclusion property.

 mvn clean install -Dsonar.coverage.exclusions=**/*ToBeExcluded.java 

This should exclude classes from the coverage calculation.

+3
Jul 09 '18 at 6:33
source share

You can configure coverage exclusion in sonar properties, outside the jacoco plugin configuration:

 ... <properties> .... <sonar.exclusions> **/generated/**/*, **/model/**/* </sonar.exclusions> <sonar.test.exclusions> src/test/**/* </sonar.test.exclusions> .... <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath> <sonar.coverage.exclusions> **/generated/**/*, **/model/**/* </sonar.coverage.exclusions> <jacoco.version>0.7.5.201505241946</jacoco.version> .... </properties> .... 

and do not forget to remove the exception settings from the plugin

+2
Aug 03 '18 at 10:16
source share

Here is a working example in the pom.xml file.

  <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <dataFile>target/jacoco.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>target/jacoco-ut</outputDirectory> <rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>PACKAGE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.00</minimum> </limit> </limits> </rule> </rules> <excludes> <exclude>com/pfj/fleet/dao/model/**/*</exclude> </excludes> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin> 
+1
Sep 28 '18 at 11:41
source share



All Articles