Java coverage reported as 0%

Hi all;

I can see this is asked multiple times but I wasn’t able to find a solution to this still. I followed the guideline here and set up the project accordingly but coverage somehow is not being reported.

Some Basic Details:

I checked coverage in my local and its properly generated:

Basic Project Info:

  • Language: Java 21 with maven
  • CI System Used: GitHub actions
  • SonarQubeCloud (aka sonarcloud)

Details or Troubleshooting:

1) pom.xml

  • related properties:

    <junit.version>4.11</junit.version>
    <jacoco.version>0.8.12</jacoco.version>
    <sonar.organization>*****</sonar.organization>
    <sonar.projectKey>>*****</</sonar.projectKey>
    <sonar.host.url>https://sonarcloud.io</sonar.host.url>
    <sonar.language>java</sonar.language>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    <sonar.verbose>true</sonar.verbose>
    <sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
    <sonar.sources>src/main/java</sonar.sources>
    <sonar.tests>src/test/java</sonar.tests>
    <sonar.java.binaries>target/classes</sonar.java.binaries>
    <sonar.exclusions>***some paths here doesnt conflict***</sonar.exclusions>
    
  • jacoco plugin config:

      <plugin>
      	<groupId>org.jacoco</groupId>
      	<artifactId>jacoco-maven-plugin</artifactId>
      	<version>${jacoco.version}</version>
      	<executions>
      		<execution>
      			<id>prepare-agent</id>
      			<goals>
      				<goal>prepare-agent</goal>
      			</goals>
      		</execution>
      		<execution>
      			<id>report</id>
      			<phase>verify</phase>
      			<goals>
      				<goal>report</goal>
      			</goals>
      			<configuration>
      				<outputDirectory>${project.basedir}/target/site/jacoco</outputDirectory>
      				<excludes>
      					<exclude>***some_eclusitions***</exclude>
      				</excludes>
      			</configuration>
      		</execution>
      	</executions>
      </plugin>
    
  • related dependencies:

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
            </dependency>
    

2) dev-ci.yml file

jobs:
  run:
env:
  SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  SONAR_MAVEN_PLUGIN_VERSION: 5.1.0.4751
runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GH_TOKEN }}
          fetch-depth: 0
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21.0.1'
          distribution: 'temurin'
      - name: Cache SonarQube packages
        uses: actions/cache@v4
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven dependencies
        uses: actions/cache@v4
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-
      - name: Build test and sonar scan
        run: |
            mvn -s .github/setting.xml clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:${{env.SONAR_MAVEN_PLUGIN_VERSION}}:sonar -Dsonar.verbose=true

Finally attaching the verbose logs of sonar:

[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[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=62ms

sonar_logs.txt (17.7 KB)

any help is appreciated, Thank You in advance!

additional info: it’s a singe module project.
and here is the latest analysis result:

Hi,

You’ve got sonar.verbose set to true in two different places, but it doesn’t seem to be picked up:

It makes me wonder where the analysis command is actually coming from. Can you dig into that & try to get verbosity actually turned on? As you can see, without it there’s not much in the log.

That said, this is a pull request analysis, and pull request analysis only reports on what was changed in the PR. So… what was changed in the PR? Based on your last screenshot, there don’t seem to have been any actual code changes? That would track with having 0% coverage: 0 code => 0 coverage.

On a side note, I’m compelled to comment on your properties:

These do nothing.

These are the standard Maven locations, so there’s no reason to specify them explicitly.

 
Ann

Hi Ann, thank you for your response.

I fixed the issue by moving all the commands to mvn arguments instead of providing them as properties in pom.xml. Sonar is failing to recognize them from the pom.xml. It seems like you guys have an issue here, i suggest to take a look at this deeper.

You’ve got sonar.verbose set to true in two different places, but it doesn’t seem to be picked up

Can you dig into that & try to get verbosity actually turned on?

That is true but is there another way to turn it on apart from what i did? I can’t see any other option even if i dig. Please provide it if you know so.

That said, this is a pull request analysis

I thought the same and I merged the PR to the main branch but there is still no coverage was being reported…until i moved everything to cmd line arguments instead of using pom.xml

These do nothing.

Then there should be no problem keeping them but removed everything just in case.

Hi,

Where, exactly, in the pom did you put the properties? They need to be in the main <properties> section.

 
Ann

they were exactly under the main <properties> tag


now they are in run commands that comes after mvn clean verify…-Dsonar…

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