Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding Lombok classes from Sonar coverage report

Latest Jacoco plugin (still in snapshot version, 0.7.10-SNAPSHOT), has a nice new feature to filter out the Lombok generated code (https://github.com/jacoco/jacoco/wiki/FilteringOptions). All we need to do is add a lombok.config file at the root of the repository with:

lombok.addLombokGeneratedAnnotation=true 

When I generate the Jacoco report internally, I see the difference. However, when my regular quality job executes and publishes the result to Sonar, I get different (ie worse) results.

How come I don't have the same results in my local report and in Sonar? Is there any workaround?

like image 669
Vincent F Avatar asked Nov 22 '17 08:11

Vincent F


2 Answers

As mentionned here : https://github.com/jacoco/jacoco/pull/513#issuecomment-293176354

filtering is performed at a time of report generation (creation of html, xml, etc), not at a time of collection of execution information (creation of exec file). So that tools that read execution data directly instead of reading of xml (which is a kind of mistake on their side to rely on purely internal intermediate format, but what's done is done) and create their own report (such as SonarQube, Jenkins, etc) will need to update their dependency on JaCoCo once it will be released in order to get filtering for reports. We will notify explicitly downstream projects (in particular all mentioned above) about this when our release will be done. So once again - please be patient. Thank you for your understanding.

I didn't find a way for Sonar to read the end report instead of the exec file, so I guess we need to be patient and wait for the official 0.7.10 jacoco plugin release and then an update on Sonar side !

------ UPDATE May 9th 2018

New versions have been released, and I can confirm it works for me.

Using :

  • Sonar 6.7
  • SonarJava plugin 5.1.1.13214
  • jacoco maven plugin 0.8.1
  • lombok.addLombokGeneratedAnnotation=true in lombok.config

I now get much better coverage results reported to Sonar, as Lombok generated code is now ignored. It really helps identifying what the "real" uncovered areas are, and whether it's risky or not.

like image 177
Vincent F Avatar answered Oct 10 '22 22:10

Vincent F


First you have to check your lombok version is at least 1.16.14

pom.xml:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
    <version>1.16.14</version>
</dependency>

Then you have to check that your Jacoco version is at least 0.8.0

pom.xml:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.0</version>
    <!-- // -->
</plugin>

Then you have to add a lombok.config file in the src folder of your project (not in the resources folder)

lombok.config:

# tells Lombok that this is the root directory and that it shouldn’t search parent directories for more configuration files
config.stopBubbling = true
# tells Lombok to add @lombok.Generated annotation to all generated methods
lombok.addLombokGeneratedAnnotation = true
like image 16
veben Avatar answered Oct 11 '22 00:10

veben