Sonarqube is not showing code coverage after running
Asked Answered
C

4

38

I'm running sonarqube with maven.

I have installed it using following way. Using brew, I installed mysql and sonar.

When I run I get 7 critical bugs but the code coverage for 88 tests is zero Sonarqube analysis

When I run it with IntelliJ's tools, I get the following results. (not zero!) IntelliJ analysis

This is when I check Jacoco results directly. In $base_direc/target/jacoco/index.html
Jacoco results

The same code when run with sonar-scannersonar-scanner


This is my maven configuration
maven configuration

My ~/.m2/settings.xml
settings-pic1
settings-pic2


Edit 1: I have found this in logs. Db not supported


Edit2: I have edited ~/.m2/settings.xml added

<properties>
    <sonar.host.url>http://localhost:9000/</sonar.host.url>
</properties>

Edited /usr/local/Cellar/sonarqube/6.3.1/libexec/conf/sonar.properties added sonar.host.url=http://localhost:9000/

Edited /usr/local/etc/sonar-scanner.properties added - sonar.host.url=http://localhost:9000/

Ran the application in all above ways and the results were same, i.e, I could see Jacoco results but not in sonar.


Is it possible that if bugs are found sonar refuses to do code coverage?!

Cambridge answered 24/5, 2017 at 11:47 Comment(3)
Anything relevant-seeming in your analysis log? If so, please edit your question to include that as well.Nave
@G.Ann-SonarSourceTeam I have found something and edited the question. Please check, is that helpfulCambridge
is there anything related to the processing of the coverage reports?Nave
C
26

I found the solution -

The maven plugin I have included has configuration of Jacoco's destfile and datafile as ${basedir}/target/coverage-reports/jacoco-unit.exec

but by default sonar reads at ${basedir}/target/jacoco.exec. I changed the default at http://localhost:9000/settings?category=java


Ref: Sonar Code Coverage


Couldn't find the working reference link. Here is aux link: Baeldung Sonar and jacoco

Cambridge answered 25/5, 2017 at 8:50 Comment(4)
Reference link is deadStamps
Life saver! Thank you for posting.Throughcomposed
Did you re-run sonarqube for the changes to take effect??Jeffereyjefferies
I don't actually remember, it was 4.5 years ago. Please check the documentation. I tried looking for it, couldn't find.Cambridge
L
5

I've resolved this by using the following steps:

1.To begin, I've add configuration in our pom.xml.

<properties>
  <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
  <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
  <sonar.language>java</sonar.language>
</properties>

2.In sonarqube properties file added the below part.

sonar.projectName=${JOB_NAME}
sonar.projectVersion=1.0.0
sonar.sources=src/main
sonar.sourceEncoding=UTF-8
sonar.language=java
sonar.tests=src/test
sonar.junit.reportsPath=target/surefire-reports
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/jacoco.exec
sonar.binaries=target/classes
sonar.java.coveragePlugin=jacoco
sonar.verbose=true

Lenora answered 22/3, 2019 at 8:7 Comment(1)
I think the sonar.jacoco.reportPath was the missing piece of the puzzle for me. I wasted about 2 days at work trying to find this. Thank you.Corral
M
1

For me, the issue was:
Reports are getting generated at pre-package phase. Now we updated Pull Request maven phase.
mvn -B clean test to mvn -B clean package.

Also, latest SONAR prefers XML report. You can specify path in maven pom via: <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

and in sonar project properties file:

sonar.projectKey=my-project
sonar.projectName=my-project

sonar.language=java
sonar.java.binaries=target/classes
sonar.sources=src/main
sonar.tests=src/test
sonar.exclusions=**/beans/*,**/config/*,**/constants/*,**/dtos/**,**/entity/*,**/exceptions/*,**/insights/*,**/App.java
sonar.sourceEncoding=UTF-8
sonar.dynamicAnalysis=reuseReports
sonar.junit.reportsPath=target/surefire-reports
sonar.java.coveragePlugin=jacoco
sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
Moneymaking answered 1/2, 2022 at 7:5 Comment(0)
G
0

I had same problem, I will help you to resolve that. Here 1st thing is to walk through your pom file.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <jacoco.version>0.8.5</jacoco.version>
    <sonar.jacoco.reportPath>target/jacoco-ut.exec</sonar.jacoco.reportPath>
    <sonar.jacoco.itReportPaths>target/jacoco-it.exec</sonar.jacoco.itReportPaths>
</properties>

In pom file, you used jacoco-ut.exec you have to use below properties on your execute SonarQube Scanner in Jenkins

sonar.java.binaries=target/classes
sonar.junit.reportsPath=target/surefire-reports
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/jacoco-ut.exec

Keep in your mind about jacoco.exec in pom and property name of executing SonarQube Scanner in Jenkins

Goner answered 26/11, 2019 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.