How can I exclude files of the Code Coverage of SonarQube using JaCoCo maven plugin

22,781

Solution 1

After a lot of reasearch, I've found the solution for this problem, I post the answers to help poeple ho'll have the same issue :

In the master-module

<properties>
    <sonar.coverage.exclusions>
       **/patternA/**/*,
       **/patternB/**/*
    </sonar.coverage.exclusions>
</properties>

In the sub-modules

<profiles>
    <profile>
        <id>report</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <!-- Exclude model classes (POJO's) -->
                            <exclude>**/patternA/**/*.class</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

in the master pom

Solution 2

I tried adding just the below details in properties section and it worked for me.

<sonar.coverage.exclusions>
     **/patternA/*.java,
     **/patternB/*.java
</sonar.coverage.exclusions>
Share:
22,781
Gilles Bodart
Author by

Gilles Bodart

Data engineer @ Utility company

Updated on January 31, 2020

Comments

  • Gilles Bodart
    Gilles Bodart over 4 years

    I've got a big issue with the integration of JaCoCo maven plugin for the code covering of SonarQube 6.0.

    I've got a multi-module Maven project lets say :

    master
        |--core
        |--web
        |--process
    

    in the master pom.xml, I've setted a reporting profile like that :

    <profile>
            <id>reporting</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>pre-unit-test</id>
                                <!--<phase>test</phase> -->
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                                <configuration>
                                    <!-- Sets the path to the file to write the execution data to. -->
                                    <destFile>${sonar.jacoco.reportPath}</destFile>
                                    <!-- Connection with SureFire plugin -->
                                    <propertyName>sonarUnitTestArgLine</propertyName>
                                </configuration>
                            </execution>
                            <execution>
                                <id>post-unit-test</id>
                                <phase>test</phase>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                                <configuration>
                                    <!-- Sets the path to where the execution data is located. -->
                                    <dataFile>${sonar.jacoco.reportPath}</dataFile>
                                    <!-- Sets the output directory for the code coverage report. -->
                                    <outputDirectory>${jacoco.ut.outputdir}</outputDirectory>
                                </configuration>
                            </execution>
    
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <forkMode>once</forkMode>
                            <argLine>${sonarUnitTestArgLine} -XX:MaxPermSize=512M -Xms512m -Xmx512m</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    

    in the childs, I overload the configuration by adding some exclusions :

    <!-- First attempt -->
    <properties>
        <sonar.jacoco.excludes>**/model/**/*</sonar.jacoco.excludes>
    </properties>
    
    <!-- Second attempt -->
    <properties>
        <sonar.coverage.exclusions>**/model/**/*</sonar.coverage.exclusions>
    </properties>
    
    <!-- Third attempt -->
    <profiles>
        <profile>
            <id>reporting</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <!-- Exclude model classes (POJO's) -->
                                <exclude>**/model/**/*.class</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    the idea here is to remove the Pojo of the code coverage ( we also do it for other types of Class ...)

    When I run the mvn command line :

    mvn clean generate-sources install verify -P reporting -fn
    

    All my reports are well generated but in Sonar, the exculsions aren't been taking into account ...

    Please can you help me fixing this issue ?

  • k0ner
    k0ner over 7 years
    I do not get it. Why do you need to do it twice?
  • Gilles Bodart
    Gilles Bodart over 7 years
    II can put it only once on the Master Pom, but I don't find that answers very robust. But it's still a way to avoid the issue ...
  • Martin Försterling
    Martin Försterling over 6 years
    @k0ner one excludes it from being instrumented by jacoco, the other hides it from the sonar report.