Search code examples
javamavensonarqubejacocojacoco-maven-plugin

Sonarqube code coverage remains 0% after configuring Jacoco


I have followed the instructions on Jacoco docs and soanrqube docs. I have my sonar server running on a self hosted runner. I have left the param sonar.coverage.jacoco.xmlReportPaths on Sonar UI empty as it was mentioned it will take the default path as target/site/jacoco/jacoco.xml. I have not set this property in my pom as well and when I locally run mvn test, I can see the report xml file generated in the same location. I can see the html page as well about my code coverage. Now I want this to be shown on my sonar UI also. As mentioned I am using a remote self hosted runner with github actions. I can see the new code added on sonarqube UI (all the changes I did to pom for jacoco) but the code coverage remains 0%.

I saw an answer to move artifacts between workflow steps but did not understand. Here is my workflow file (anything removed from file is replaced by ...):

name: SonarQube

on:
  workflow_dispatch:
  pull_request:

jobs:

  scanning:
    runs-on: self-hosted
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up JDK 21
        uses: actions/setup-java@v1
        with:
          java-version: 21

      - name: Configure Maven settings
        uses: whelk-io/maven-settings-xml-action@v22
        with:......

      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        env:
          SONAR_TOKEN: ${{ ... }}
          SONAR_HOST_URL: ${{ ... }}
        run: mvn -B verify -DskipTests org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=... -Dsonar.projectName='...'

In pom.xml, I added plugin and dependency, plugin:

<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>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Properties in pom.xml:

<jacoco.version>0.8.10</jacoco.version>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>

Dependency in pom.xml:

<dependency>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.11</version>
    </dependency>

I have also tried giving same path to sonar.coverage.jacoco.xmlReportPaths in pom properties and on sonar gui and it still didn't work. I dont have any logs of error although on server I can see in sonar container logs:

2024.05.06 10:44:06 INFO  ce[b5aba757-5a3e-4285-bddc-eda685e8126e][o.s.c.t.s.ComputationStepExecutor] Compute new coverage | status=SUCCESS | time=34ms
2024.05.06 10:44:06 INFO  ce[b5aba757-5a3e-4285-bddc-eda685e8126e][o.s.c.t.s.ComputationStepExecutor] Compute coverage measures | status=SUCCESS | time=1ms
2024.05.06 10:44:06 INFO  ce[b5aba757-5a3e-4285-bddc-eda685e8126e][o.s.c.t.s.ComputationStepExecutor] Compute comment measures | status=SUCCESS | time=0ms

enter image description here

enter image description here


Solution

  • -DskipTests was the problem in my workflow command