来自JaCoCo Maven插件的SonarQube集成测试覆盖率显示0%

Mic*_* K. 14 configuration integration-testing maven sonarqube jacoco-maven-plugin

我们有一个多模块多语言maven java项目,使用jacoco进行覆盖分析.模块的主要部分是带有REST API的后端(Java代码),我们的webapp模块包含前端(AngularJS)和java中的Integration-Tests.我们的Jacoco-IT.exec文件包含大约100Kb的数据,因此我们猜测可以收集集成测试的一些Coverage数据.然而,我们无法在SonarQube中看到任何报道(使用版本5.0和4.5)

我们运行构建项目并运行集成测试

mvn clean install
Run Code Online (Sandbox Code Playgroud)

用数据分析数据

mvn sonar:sonar
Run Code Online (Sandbox Code Playgroud)

项目配置为多语言,但我们只是分析了java代码(我们曾经有过单语言配置,但与覆盖数据相关的结果没有差异)

我们的父母POM:

<properties>
    <hibernate.version>4.2.2.Final</hibernate.version>
    <spring.version>3.2.3.RELEASE</spring.version>
    <resteasy.version>3.0.4.Final</resteasy.version>
    <cucumber.version>1.1.3</cucumber.version>
    <selenium-java.version>2.33.0</selenium-java.version>
    <!-- Sonar -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
    <jacoco.dataFile>${project.basedir}/../target/jacoco-it.exec</jacoco.dataFile>
</properties>
<build>
   <plugins>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <debug>true</debug>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>

            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report-integration</id>
                    <goals>
                        <goal>report-integration</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
        </plugin>
    </plugins>
    <pluginManagement>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.2.201409121644</version>
            </plugin>
    </pluginMangement>
</build>
Run Code Online (Sandbox Code Playgroud)

我们使用集成测试的webapp中的POM:

<properties>
    <sonar.sources>src/main</sonar.sources>
</properties>
<build>
    <finalName>web</finalName>
    <plugins>
        <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.7.v20120910</version>
        <configuration>...</configuration>
        <executions>...</executions>
    </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <!-- The destination file for the code coverage report has to be set to the same value
                in the parent pom and in each module pom. Then JaCoCo will add up information in
                the same report, so that, it will give the cross-module code coverage. -->
                <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

正如您在我们的父POM中看到的那样,我们将itReportPath配置为位于所有模块的同一目录中.因此,当Sonar分析所有模块时,它总是读取相同的覆盖数据,并且应该将它们与当前模块的二进制文件中的调试信息对齐.在maven(mvn sonar:sonar)的分析阶段,我们得到一些消息,我们不确定它们是否是一个问题:

对于所有java模块,我们得到以下几行:

    [INFO] [17:30:42.992] Sensor SurefireSensor...
    [INFO] [17:30:42.992] parsing \git\airport-core\rest\target\surefire-reports
    [INFO] [17:30:43.009] Sensor SurefireSensor done: 17 ms
    [INFO] [17:30:43.009] Sensor JaCoCoItSensor...
    [INFO] [17:30:43.010] Analysing \git\airport-core\rest\..\target\jacoco-it.exec
    [INFO] [17:30:43.018] No information about coverage per test.
    [INFO] [17:30:43.018] Sensor JaCoCoItSensor done: 9 ms
    [INFO] [17:30:43.018] Sensor JaCoCoOverallSensor...
    [INFO] [17:30:43.027] Analysing \git\airport-core\rest\target\sonar\jacoco-overall.exec
    [INFO] [17:30:43.046] No information about coverage per test.
    [INFO] [17:30:43.046] Sensor JaCoCoOverallSensor done: 28 ms
Run Code Online (Sandbox Code Playgroud)

是"没有关于每次测试覆盖率的信息." 一个问题?好吧,我们不知道哪个测试导致覆盖率,但我们应该在SonarQube中获得不同的值而不是0%

对于我们的webapp模块,除了集成测试源本身之外,它不包含任何java源/类,我们得到以下行:

    [INFO] [17:30:48.370] Sensor JaCoCoItSensor...
    [INFO] [17:30:48.370] No JaCoCo analysis of project coverage can be done since there is no class files.
    [INFO] [17:30:48.370] Sensor JaCoCoItSensor done: 0 ms
Run Code Online (Sandbox Code Playgroud)

这应该不是问题,因为此模块中没有类.这个观察是否正确?

对于主要部分,我们的配置基于官方SonarCube演示配置(https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/combined%20ut-it/combined- ut-it-multimodule-maven-jacoco),我们刚刚添加了"准备 - 代理 - 集成"目标.当然,它改为多语言项目.

我们在网上发现了很多被删除的信息(argLine设置等),这些信息对我们没有任何改变.

链接到我们使用的文档和教程:

Ste*_*vra 1

我在为Jersey配置 JaCoCo 时也有同样的经历。我最终应用了二分法来找出哪些模块导致整个项目的代码覆盖率降至 0%。

如果我没记错的话,最大的惊喜是maven-shade-plugin它以某种方式破坏了收集的覆盖率和类位置之间的关联,导致 Sonar 显示 0% 的代码覆盖率。我最终通过将其绑定到阶段none来禁用其执行以修复它(请参阅此提交)。

作为示例,这是如何扩展pom.xml子模块的 a 以获得工作声纳(假设声纳是使用sonarMaven 配置文件执行的)。

<profile>
    <id>sonar</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- disable parallel execution so that JaCoCo listener can properly work -->
                    <parallel>none</parallel>
                    <perCoreThreadCount>false</perCoreThreadCount>
                    <forkCount>1</forkCount>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <id>shade-archive</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm-debug-all</artifactId>
            <optional>false</optional>
        </dependency>
    </dependencies>
</profile>
Run Code Online (Sandbox Code Playgroud)