4
votes

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)

1
Did you have a look at the SonarQube log files?Jeroen Heier
@JeroenHeier I can't see the log as I have no control of that SonarQube server (it's managed by another team)hydradon
What does the log of the analysis tells you ? why don't you use SonarQube scanner for gradle ? SonarRunner is deprecated if i'm correct you should be using sonar scanner anyway.benzonico
@benzonico Sonar Runner is a dedicated build step in TeamCity.hydradon
Also, the admin of SonarQube server prevents any local analyis results from being pushed directly to serverhydradon

1 Answers

3
votes

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