JaCoCo analysis via sonar-scanner-cli

Using sonar-scanner-cli via Docker (sonarsource/sonar-scanner-cli:4.4).

TL;DR: How do I get JaCoCo working with sonar-scanner-cli and not using the Gradle plugin? Am I missing system properties?

So I have a pretty minimal sonar-project.properties file and it seems to be working quite well for my Kotlin projects. The only problem that I’m currently facing is that JaCoCo is not updating.

But seems like it’s getting imported correctly.

INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=111ms

I’m aware of the Gradle plugin, but I’m hesitant to use it because I’m using a custom GitHub Action workflow to make reporting to SonarQube more general.

In any case, everything seems to be working great (PRs, branches, etc), but code coverage for JaCoCo. Even works with other languages like golang.

I’m getting the feeling that I’m missing a system property that is necessary to provide for coverage reporting that the Gradle plugin provides but not the sonar-scanner-cli.

sonar-project.properties:

sonar.projectKey=myproject
sonar.sources=src/
sonar.exclusions=src/test/

And part of the workflow. Should provide enough context.

if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
    sonar-scanner \
        -D"sonar.host.url=${INPUT_SONARHOSTURL}" \
        -D"sonar.scm.provider=git" \
        -D"sonar.login=${INPUT_SONARTOKEN}" \
        -D"sonar.pullrequest.key=${PR_KEY}" \
        -D"sonar.pullrequest.branch=${GITHUB_HEAD_REF}" \
        -D"sonar.pullrequest.base=${GITHUB_BASE_REF}" \
        -D"sonar.pullrequest.github.repository=${GITHUB_REPOSITORY}"
elif [ "$GITHUB_EVENT_NAME" = "push" ]; then
    sonar-scanner \
        -D"sonar.host.url=${INPUT_SONARHOSTURL}" \
        -D"sonar.projectVersion=${INPUT_GITTAG}" \
        -D"sonar.scm.provider=git" \
        -D"sonar.login=${INPUT_SONARTOKEN}" \
        -D"sonar.branch.name=${_branch_name}"
fi

Seems like sonar.java.binaries may be necessary. Although I believe this gets picked up automatically.

I ended up editing my configuration and now receive code coverage without the Gradle plugin.

sonar.sources=src/main
sonar.tests=src/test
sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
sonar.java.binaries=build/classes/kotlin/main
sonar.java.test.binaries=build/classes/kotlin/test

Hello @jef,

Glad to see that you managed to find a solution to your problem.

For the record, I am skeptical about the fact that sonar.java.binaries was the problem here, it is used only for a project containing .java files, and should not impact the coverage import of Kotlin in any way.
I tested just now, and without setting any property, sonar-scanner-cli is able to import it automatically.

Note:

sonar.java.binaries may be necessary. Although I believe this gets picked up automatically.

Not when you are using sonar-scanner-cli, and in fact, it is one of the benefits to using the Gradle scanner :wink:
The coverage report should be picked-up automatically though.

Interesting… So you’re saying that in your trial, you only had sonar.projectKey (or to this extent)?

Maybe it’s because I’ve included sonar.tests… I feel like it was showing before. But if that’s the case, maybe I’ll just wipe the config completely and see if it works.

So you’re saying that in your trial, you only had sonar.projectKey (or to this extent)?

In a simple project, yes, but it does not mean that other properties are useless, they can influence what is displayed at the end.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.