Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 232
Gilles Bodart Avatar asked Jan 18 '17 16:01

Gilles Bodart


People also ask

How do I exclude some files from code coverage in SonarQube?

You can prevent some files from being taken into account for code coverage by unit tests. To do so, go to Project Settings > General Settings > Analysis Scope > Code Coverage and set the Coverage Exclusions property.

How do I exclude a package from code coverage in JaCoCo?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.

Does sonar use the JaCoCo Exclusions List?

Yes, presumably. SonarQube reads the report that JaCoCo gives it. If JaCoCo excludes certain classes, then SonarQube shouldn't get any data on them.

What is SonarQube and JaCoCo?

SonarQube is an open-source and standalone service that provides an overview of the overall health of our source code by measuring code quality and code coverage. In this tutorial, we'll cover the process of measuring code coverage using SonarQube and JaCoCo. 2. Description 2.1. Code Coverage

What is the JaCoCo Maven plugin?

Dependencies and Plugins for JaCoCo The JaCoCo Maven plugin provides access to the JaCoCo runtime agent, which records execution coverage data and creates a code coverage report. Now, let's have a look at the dependency we'll add to our pom.xml file:

What is SonarQube in Maven?

SonarQube is used in integration with JaCoCo, a free code coverage library for Java. 3. Maven Configuration 3.1. Download SonarQube We can download SonarQube from its official website.

How do I view the code coverage of a SonarQube project?

Viewing the SonarQube report details We can head back to SonarQube at localhost:9000/projects to see the test code coverage report. We can see a reported code coverage of 66.7%. Awesome! Click on the sonarqube-jacoco-code-coverage link and we’ll try to drill into exactly how this was calculated.


2 Answers

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

like image 136
Gilles Bodart Avatar answered Sep 17 '22 13:09

Gilles Bodart


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>
like image 34
Himadri Avatar answered Sep 19 '22 13:09

Himadri