3

I am using Jacoco offline instrumentation as I have used PowerMockito for my test cases. Mine is Maven Multi Module Project. I am using PowerMockito 1.65 My SonarQube version is 6.7

JaCoCo generates HTML files in every module, But in SonarQube it is showing 0%

My parent pom.xml

<properties>
    <sonar.version>6.7</sonar.version>
    <sonar-java.version>4.2</sonar-java.version>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
</properties>

<dependencies>
    <dependency>
        <groupId>org.sonarsource.sonarqube</groupId>
        <artifactId>sonar-plugin-api</artifactId>
        <version>${sonar.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.sonarsource.java</groupId>
        <artifactId>sonar-java-plugin</artifactId>
        <version>5.0.1.12818</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>org.jacoco.agent</artifactId>
        <classifier>runtime</classifier>

        <version>0.7.9</version>
    </dependency>

    <dependency>
        <groupId>org.sonarsource.java</groupId>
        <artifactId>sonar-jacoco-listeners</artifactId>
        <version>4.9.0.9858</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>2.6.1</version>
        </plugin>

    </plugins>
</build>
<profiles>
    <profile>
        <id>sonar-coverage</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>

            <plugins>


                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <configuration>
                        <append>true</append>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-instrument</id>
                            <goals>
                                <goal>instrument</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-restore-instrumented-classes</id>

                            <goals>
                                <goal>restore-instrumented-classes</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>

                            </goals>
                            <configuration>
                                <dataFile>${project.build.directory}/coverage.exec</dataFile>
                            </configuration>
                        </execution>

                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<modules>
    <module>module1</module>
    <module>module2</module>
</modules>

run with command clean install test sonar:sonar

JaCoCo Report enter image description here

SonarQube Report enter image description here

Pradeep Charan
  • 653
  • 2
  • 7
  • 28

1 Answers1

4

If you change the default file location for the coverage file which for the maven jacoco plugin is:

${project.build.directory}/jacoco.exec

You also need to tell the sonar plugin where to find it. Sonar uses a lot of properties for the plugin, the jacoco report file should be: sonar.jacoco.reportPaths Once the sonar plugin looks into the correct location it should work. Note to calculate the coverage sonar uses both the coverage files and the compiled classes.

Just a side note: you execute mvn clean install test sonar:sonar which is a bit redundant, since test is a phase before install an install would do the same. See Maven Life-Cycle. But that should not matter here.

wemu
  • 7,952
  • 4
  • 30
  • 59