4

I have a gradle project with some codes in src/main/java and some unit tests in src/test/java

Below is snippet from build.gradle

apply plugin: "jacoco"

sourceSets {
    main {
        java { srcDir 'src/main/java' }
    }
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpDir = file("$buildDir/jacoco/classpathdumps")
    }
}

jacoco {
    toolVersion = "0.7.8"
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/reports/jacoco/jacocoHtml"
    }
}

On TeamCity, I have 2 steps, first is Gradle step with command gradle clean jacocoTestReport build and second step is SonarQube runner with the following parameters:

-Dsonar.sources=%system.teamcity.build.checkoutDir%/src
-Dsonar.java.binaries=%system.teamcity.build.checkoutDir%/build/classes
-Dsonar.branch.name=%teamcity.build.branch%
-Dsonar.jacoco.reportPaths=%system.teamcity.build.checkoutDir%/build/jacoco/jacocoTest.exec

However, on SonarQube dashboard, my project still shows to have 0% Coverage. Please advise me if I feed jacoco coverage report correctly to SonarQube (Version 6.7)

hydradon
  • 1,316
  • 1
  • 21
  • 52

1 Answers1

3

If you are using TeamCity JaCoCo integration for Gradle runner the coverage should be automatically. Make sure you've provided "Binaries location:" in the SonarQube Runner. In this case the build log will contain such lines:

 # before SonarQube start:
 -Dsonar.java.coveragePlugin=jacoco
 -Dsonar.jacoco.reportPath=/.../buildAgent/temp/buildTmp/JACOCO8457480821230827929coverage/jacoco.exec

 # while SonarQube is executed:
 Sensor JaCoCoSensor
 Analysing /.../buildAgent/temp/buildTmp/JACOCO8457480821230827929coverage/jacoco.exec

In case you want to use JaCoCo plugin in your Gradle script you should manually set coverage type and data location in the SonarQube Runner step

-Dsonar.java.coveragePlugin=jacoco
-Dsonar.jacoco.reportPaths=%system.teamcity.build.checkoutDir%/build/jacoco/jacocoTest.exec

So try to add "sonar.java.coveragePlugin" property

linfar
  • 76
  • 2
  • HI @linfar, May i check that for binaries location, will it be location of the compiled classes or the instrumented classes? – hydradon Dec 08 '17 at 02:56
  • @hydradon I've never run JaCoCo in offline mode. [Documentation](http://www.jacoco.org/jacoco/trunk/doc/offline.html) says you have to target initial compiled classes, not instrumented. – linfar Dec 14 '17 at 07:50