Does sonar use the jacoco exclusions list?

9,006 views
Skip to first unread message

julian...@gmail.com

unread,
Jan 13, 2017, 6:34:32 PM1/13/17
to SonarQube
Hi Everyone, 

I've been using using sonar at work on a large project with a large number of developers for over two years. I'm on sonar server 5.5. I'm really thankful for the help this community has provided in the past. 

I can see that Sonar has it's own exclusions list property for coverage. 

My question is: Does sonar use the jacoco exclusions list for coverage?


Cheers
Julian

G. Ann Campbell

unread,
Jan 16, 2017, 2:58:51 AM1/16/17
to SonarQube, julian...@gmail.com
Hi Julian,

Does SonarQube use this property? No.

Does it get used anyway? Yes, presumably.

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

OTOH, SonarQube 6.2 implemented, among other things, "force coverage to 0" functionality. So for languages where "executable lines" data is provided by the language plugin (and it is provided by SonarJava), those classes excluded in the JaCoCo report will show up as having 0 coverage in SonarQube. So your best bet is to exclude the classes in SonarQube, or perhaps in both places.


Ann

robert...@gmail.com

unread,
Jan 28, 2018, 4:34:05 PM1/28/18
to SonarQube
G. Ann,

How can I make SonarQube completely ignore the classes I excluded?
Please correct me if I am wrong: I believe that by setting the excluded
classes' coverage to 0, sonar will treat them as "issues", meaning that
it expects unit tests to be written for them and it is not finding such tests.
This will lower the overall coverage percentage, which is undesirable for
me since I only want Sonar to analyse the classes I am working with
(non-excluded ones).

I am working with Sonar 6.5 and I see that it is possible to remove those
classes globally but I want to do it in per-application basis, which is
something that Jacoco allows me to do. After removing some classes,
I see in the Maven logs that Jacoco is indeed removing them but it made
no difference in Sonar. I used the "sonar.jacoco.reportMissing.force.zero=true"
parameter for this test.

Thank you,

Rob.

Julien HENRY

unread,
Jan 29, 2018, 2:58:07 AM1/29/18
to SonarQube
Hi Rob,

To tell SonarQube to completely ignore coverage of some files, you should use the parameter sonar.coverage.exclusions.

++

Julien
Message has been deleted

robert...@gmail.com

unread,
Jan 29, 2018, 5:32:20 PM1/29/18
to SonarQube
Julien,

Thanks, it worked.
To make it work, I added to the pom.xml file all the exclusions in the same line of the "sonar.coverage.exclusions" property with the paths starting from the app's root and using the .JAVA extension.
I also added the files to the Jacoco plugin excludes section, with each path starting from the beginning of the package and using the .CLASS extension.
Using the maven command line (mvn clean verify sonar:sonar -Dsonar.coverage.exclusions=...) also worked but I prefer to add such options to the pom file for cleanness sake.
As of a suggestion, it would be nice if you could make it possible to add the exclusions in multiple lines, separated by commas. I tried that way and it only recognized the first line and the first comma.

See the full configuration below.

<properties>
    ...
    <sonar.coverage.exclusions> <!-- the members of the following list should be in the same line -->
        src/main/java/nz/co/company/project/application/dto/*.java,src/main/java/nz/co/company/project/application/exception/*.java
    </sonar.coverage.exclusions>
    ...
</properties>
...
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.0</version>
    <configuration>
        <excludes>
            <exclude>nz/co/company/project/application/dto/*.class</exclude>
            <exclude>nz/co/company/project/application/exception/*.class</exclude>
        </excludes>
        <destFile>${sonar.jacoco.reportPath}</destFile>
    </configuration>
    <executions>
    ...
    </executions>
</plugin>
...

Regards,

Rob.

Julien HENRY

unread,
Jan 30, 2018, 3:29:29 AM1/30/18
to robert...@gmail.com, SonarQube
Hi Rob,

That's correct, JaCoCo exclusions are "classname based" while on SonarQube side we are working with files path.
 
As of a suggestion, it would be nice if you could make it possible to add the exclusions in multiple lines, separated by commas. I tried that way and it only recognized the first line and the first comma.
 
This is supposed to be supported. We had a regression for this in SonarQube 6.5, but it was fixed in 6.6: https://jira.sonarsource.com/browse/SONAR-9666
What is your SonarQube version?

++
Reply all
Reply to author
Forward
0 new messages