10

I am using the Jacoco Maven plugin version 0.8.1 (With Java 8 / Maven 3.1.0). Cannot get Jacoco to work with path exclusions. I would like to exclude these packages :

  • my.package.model
  • my.package.exception

What I tried :

<build>
      <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>  
                    </execution>
                </executions>
                <configuration>
                    <excludes>
                        <exclude>my.package.model</exclude>
                        <exclude>my.package.exception</exclude>
                    </excludes>
                </configuration>
            </plugin>
      </plugins>
</build>

<reporting>
        <plugins>
             <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <configuration>
                    <excludes>
                        <exclude>my.package.model</exclude>
                        <exclude>my.package.exception</exclude>
                    </excludes>
                </configuration>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
</reporting>

And also (for the excludes part) :

<excludes>
     <exclude>my.package.model**</exclude>
     <exclude>my.package.exception**</exclude>
</excludes>

<excludes>
     <exclude>my/package/model/**/*</exclude>
     <exclude>my/package/exception/**/*</exclude>
</excludes>

<excludes>
     <exclude>*/my/package/model/**/*</exclude>
     <exclude>*/my/package/exception/**/*</exclude>
</excludes>

<excludes>
     <exclude>**/my/package/model/**/*</exclude>
     <exclude>**/my/package/exception/**/*</exclude>
</excludes>

<excludes>
     <exclude>**/my/package/model/**</exclude>
     <exclude>**/my/package/exception/**</exclude>
</excludes>

<excludes>
     <exclude>**/my/package/model/*</exclude>
     <exclude>**/my/package/exception/*</exclude>
</excludes>

<excludes>
     <exclude>**/model/*</exclude>
     <exclude>**/exception/*</exclude>
</excludes>

<excludes>
     <exclude>**/model**</exclude>
     <exclude>**/exception**</exclude>
</excludes>

Impossible to remove these packages from the final report ... Frustrating.

I read :

Nothing work.

Any idea ?

cactuschibre
  • 1,908
  • 2
  • 18
  • 36

2 Answers2

12

I have that and it works (notice that the excludes are in execution/configuration and that everything is in pluginManagement):

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                    <configuration>
                        <destFile>${project.build.directory}/integration-coverage-reports/jacoco-it.exec</destFile>
                        <excludes>
                            <exclude>weblogic/*</exclude>
                        </excludes>
                    </configuration>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/integration-coverage-reports/jacoco-it.exec</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                        <excludes>
                            <exclude>my/company/package/db/entity/**/*.class</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</pluginManagement>
Shadov
  • 5,421
  • 2
  • 19
  • 38
  • 1
    Ok thanks ! So I have updated my `` section, removed the configuration from `` section and added the full path of package with `*.class`. It was necessary to add the `class` extension. – cactuschibre Jun 29 '18 at 09:23
  • The folder structure also matters : for example : **/webapp/**/* will exclude all files under webapp directory. The "**" is for directory and "*" is for file. If you just have **/webapp/* then it won't exclude the folders under webapp folder. – Smart Coder Jun 26 '20 at 15:53
  • Looks like sonar exclusion is also needed if you want to exclude from sonar coverage. By including this in properties section which is picked up by Sonar automatically : **/domains/**/*, **/webapp/**/* – Smart Coder Jun 26 '20 at 19:41
5

As per the example that you have shared, this change in the POM file should work:

<build>
      <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>  
                    </execution>
                </executions>
                <configuration>
                    <excludes>
                        <exclude>my/package/model/**/*.class</exclude>
                        <exclude>my/package/exception/**/*.class</exclude>
                    </excludes>
                </configuration>
            </plugin>
      </plugins>
</build>

<reporting>
        <plugins>
             <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <configuration>
                    <excludes>
                        <exclude>my/package/model/**/*.class</exclude>
                        <exclude>my/package/exception/**/*.class</exclude>
                    </excludes>
                </configuration>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
</reporting>

I usually put the prepare-agent goal and report goal inside the same plugin. This helps me to put the exclude packages under one section only. This is how my pom file looks:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.1</version>

            <configuration>
                <excludes>
                    <exclude>my/package/model/**/*.class</exclude>
                    <exclude>my/package/exception/**/*.class</exclude>
                </excludes>
            </configuration>

            <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>
            </executions>
        </plugin>
    </plugins>
</build>
Rito
  • 3,092
  • 2
  • 27
  • 40