SonarQube 9.1- Coverage report is 0%

SonarQube 9.1.

All work fine , except coverage tests. Coverage = 0%

SonarQube config is in Jenkins’ job:

sonar:sonar 
sonar.analysis.mode=publish
sonar.issuesReport.html.enable=true
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
sonar.junit.reportsPath=**/target/surefire-reports/*
sonar.java.binaries=**/target/classes/**
sonar.java.source=1.8
# Tells SonarQube where the unit tests code coverage report is
sonar.jacoco.reportPath=**/target/jacoco-ut.exec

# Encoding of the source files
sonar.sourceEncoding=UTF-8

Here pom.xml

<plugin>
						<groupId>org.jacoco</groupId>
						<artifactId>jacoco-maven-plugin</artifactId>
						<version>0.8.8</version>
						<executions>
<!-- 							Prepares a variable, jacoco.agent.ut.arg, that contains the info to be passed to the JVM hosting the code being tested. -->
							<execution>
								<id>prepare-ut-agent</id>
	            				<phase>process-test-classes</phase>
								<goals>
	              					<goal>prepare-agent</goal>
								</goals>
								<configuration>
	              					<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
	              					<propertyName>jacoco.agent.ut.arg</propertyName>
									<append>true</append>
								</configuration>
							</execution>
<!-- 							Prepares a variable, jacoco.agent.it.arg, that contains the info to be passed to the JVM hosting the code being tested. -->
							<execution>
								<id>prepare-it-agent</id>
								<phase>pre-integration-test</phase>
								<goals>
									<goal>prepare-agent</goal>
								</goals>
								<configuration>
									<destFile>${project.build.directory}/jacoco-it.exec</destFile>
									<propertyName>jacoco.agent.it.arg</propertyName>
									<append>true</append>
								</configuration>
							</execution>
						</executions>
					</plugin>

P.S. After success finish run tests the file jacoco-ut.exec is not exist. But folder surefire-reports success generated.

Hey there.

Let me suggest you read through this guide:

Here my new version of pom.xml

	<profile>
			<id>coverage</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-surefire-plugin</artifactId>
						<configuration>
							<argLine>-Xmx4096m ${jacoco.agent.ut.arg}</argLine>
<!-- 							Specific to generate mapping between tests and covered code -->
							<properties>
								<property>
									<name>listener</name>
									<value>org.sonar.java.jacoco.JUnitListener</value>
								</property>
							</properties>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-failsafe-plugin</artifactId>
						<configuration>
							<argLine>-Xmx4096m ${jacoco.agent.it.arg}</argLine>
<!-- 							Specific to generate mapping between tests and covered code -->
							<properties>
								<property>
									<name>listener</name>
									<value>org.sonar.java.jacoco.JUnitListener</value>
								</property>
							</properties>
<!-- 							Let's put failsafe reports with surefire to have access to tests failures/success reports in sonar -->
							<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
						</configuration>
					</plugin>
					<plugin>
						<groupId>org.jacoco</groupId>
						<artifactId>jacoco-maven-plugin</artifactId>
						<version>0.7.4.201502262128</version>
						<executions>
<!-- 							Prepares a variable, jacoco.agent.ut.arg, that contains the info to be passed to the JVM hosting the code being tested. -->
							<execution>
								<id>prepare-ut-agent</id>
	            				<phase>process-test-classes</phase>
								<goals>
	              					<goal>prepare-agent</goal>
								</goals>
								<configuration>
	              					<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
	              					<propertyName>jacoco.agent.ut.arg</propertyName>
									<append>true</append>
								</configuration>
							</execution>
<!-- 							Prepares a variable, jacoco.agent.it.arg, that contains the info to be passed to the JVM hosting the code being tested. -->
							<execution>
								<id>prepare-it-agent</id>
								<phase>pre-integration-test</phase>
								<goals>
									<goal>prepare-agent</goal>
								</goals>
								<configuration>
									<destFile>${project.build.directory}/jacoco-it.exec</destFile>
									<propertyName>jacoco.agent.it.arg</propertyName>
									<append>true</append>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
			<dependencies>
				<dependency>
					<groupId>org.codehaus.sonar-plugins.java</groupId>
					<artifactId>sonar-jacoco-listeners</artifactId>
					<version>1.5</version>
					<scope>test</scope>
				</dependency>
			</dependencies>
		</profile>

I run this:

mvn clean install -Pcoverage org.jacoco:jacoco-maven-plugin:prepare-agent
-Dmaven.test.failure.ignore=true
-Djacoco.outputDir=${WORKSPACE}/target

And as result success generate file jacoco.exec in all folders target. Nice. But in Sonar the coverage is again 0%

Hey there.

You don’t seem to use the example laid out in the previous guide, so I’m not sure what other help we can be. :person_shrugging:

SonarQube only reads coverage reports, it isn’t responsible for generating them – but SonarQube expects a specific format (JaCoCo XML reports) rather than the .exec reports that are ultimately converted to XML files. This is the job of the report goal noted in the previous guide.

<executions>
  <execution>
    <id>prepare-agent</id>
    <goals>
     <goal>prepare-agent</goal>
    </goals>
  </execution>
  <execution>
    <id>report</id>
    <goals>
     <goal>report</goal>
    </goals>
  </execution>
</executions> 

I would suggest that you focus on first generating the JaCoCo XML reports required, and then see if anything additional is needed to import the reports (if they land in the default location, as also noted in the guide, no configuration is needed)

In pom.xml now is

<executions>
  <execution>
    <id>prepare-agent</id>
    <goals>
     <goal>prepare-agent</goal>
    </goals>
  </execution>
  <execution>
    <id>report</id>
    <goals>
     <goal>report</goal>
    </goals>
  </execution>
</executions>

Ok, now in my local host success generate jacoco’s report in xml format. Nice.

But what about SonarQube? What about this settings in jenkins’s job:

sonar.jacoco.reportPath=**/target/jacoco-ut.exec

The question is: Is I need to change it to smt like this:

sonar.jacoco.reportPath=**/target/site/jacoco/jacoco.xml

or like this:
sonar.coverage.jacoco.xmlReportPaths=**/target/site/jacoco/jacoco.xml

?