SonarQube Jacoco离线仪器在Maven Multi Module Project中显示0%的覆盖率

时间:2017-12-30 22:50:15

标签: maven sonarqube jacoco-maven-plugin

我正在使用Jacoco离线工具,因为我已将PowerMockito用于我的测试用例。我的是Maven多模块项目。 我使用的是PowerMockito 1.65 我的SonarQube版本是6.7

JaCoCo在每个模块中生成HTML文件,但在SonarQube中它显示0%

我的父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>

使用命令clean install test sonar:sonar

运行

JaCoCo报告 enter image description here

SonarQube报告 enter image description here

1 个答案:

答案 0 :(得分:3)

如果您更改maven jacoco plugin的覆盖文件的默认文件位置:

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

你还需要告诉声纳插件在哪里找到它。 Sonar使用了很多properties for the plugin,jacoco报告文件应为:sonar.jacoco.reportPaths 一旦声纳插件查看到正确的位置,它应该工作。注意计算覆盖率声纳使用覆盖文件和编译的类。

请注意:执行mvn clean install test sonar:sonar这有点多余,因为测试是安装之前的一个阶段,安装会做同样的事情。见Maven Life-Cycle。但这不重要。