2
votes

I'm using Gradle 4.4 on my Java project with JaCoCo 0.8.0, and Sonar with SonarJava 5.0.1.

I have a class annotated with lombok's @Value and @Builder.

My JaCoCo config in build.gradle looks like this:

jacoco {
    toolVersion = "0.8.0"
    reportsDir = file("$buildDir/reports/jacoco")
}

jacocoTestReport.doFirst{
    classDirectories = files("buildDir/classes")
}

task jacocoReport(type: JacocoReport){
    sourceSets sourceSets.main
    executionData test, integrationTest
}

Also, I have lombok.confi file, with property lombok.addLombokGeneratedAnnotation = true, and generated stuff does actually have @lombok.Generated annotation in build/classes.

However, the coverage on Sonar is still low. It reports a ton of conditions and lines to cover.

2

2 Answers

3
votes

As stated in announcement of release of JaCoCo version 0.8.0:

Please Note

Tools that directly read exec files (which is not a final report) and embed JaCoCo for generation of report will provide filtering functionality only after they updated to this version of JaCoCo.

So please follow/wait/etc respective vendors such as SonarQube - https://jira.sonarsource.com/browse/SONARJAVA-2608

Reports generated by corresponding version (0.8.0) of integrations developed as part of JaCoCo project by us (Ant Tasks, Maven Plugin and Command Line Interface) provide filtering functionality.

With Gradle JaCoCo Plugin you can select both runtime and version for "JaCoCoReport" task using "toolVersion" - https://docs.gradle.org/current/userguide/jacoco_plugin.html

This is also stated in JaCoCo changelog:

Note: Tools that directly read exec files and embed JaCoCo for this (such as SonarQube or Jenkins) will provide filtering functionality only after they updated to this version of JaCoCo.

As of today (29 Jan 2018) fix for https://jira.sonarsource.com/browse/SONARJAVA-2608 is supposed to be in not yet released SonarJava plugin version 5.1.

From all the above: report generated by Gradle should already be filtered, report generated by SonarQube will be filtered after upgrade of SonarJava.

2
votes

Apart from configuring lombok, I also had to tell sonar scanner where to find the dependencies. Was the only way to get rid of the "Unused private fields should be removed" message in SonarQube.

I followed instructions from this site: https://netwolfuk.wordpress.com/2017/10/29/maven-sonarqube-jacoco-lombok-and-teamcity/

It boils down to:

  1. Tell maven to copy its dependencies to somewhere Sonar can find them. This is simple as adding:

    dependency:copy-dependencies
    
  2. Add the following to the sonar build step:

    -Dsonar.java.libraries=target/dependency/*.jar