无法让sonar处理多模块maven项目的jacoco exec文件

无法让sonar处理多模块maven项目的jacoco exec文件,maven,sonarqube,jacoco,Maven,Sonarqube,Jacoco,我试图为sonar的多模块maven项目生成单元测试代码覆盖率数据,但没有得到正确的结果 项目结构类似于以下内容: 计划 亲本 模 模B 模块B1 模块B2 模块C 我使用jacoco maven插件生成jacoco.exec文件,使用sonar maven插件分析代码(包括jacoco.exec文件) 这些文件在过程测试阶段生成,如下所示: <properties> <jacoco.report.path>${project.build.director

我试图为sonar的多模块maven项目生成单元测试代码覆盖率数据,但没有得到正确的结果

项目结构类似于以下内容:

  • 计划
    • 亲本
    • 模B
      • 模块B1
      • 模块B2
    • 模块C
我使用jacoco maven插件生成
jacoco.exec
文件,使用sonar maven插件分析代码(包括
jacoco.exec
文件)

这些文件在
过程测试
阶段生成,如下所示:

<properties>
  <jacoco.report.path>${project.build.directory}/jacoco.exec</jacoco.report.path>
  <sonar.jacoco.reportPath>${jacoco.report.path}</sonar.jacoco.reportPath>
</properties>

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.4.201502262128</version>
  <executions>
    <execution>
      <id>default-prepare-agent</id>
      <phase>process-test-classes</phase>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${jacoco.report.path}</destFile>
      </configuration>
    </execution>
  </executions>
</plugin>
<properties>
  <!-- single jacoco.exec file relative to root directory of the project -->
  <jacoco.report.path>${session.executionRootDirectory}/code-coverage/jacoco.exec</jacoco.report.path>
  <sonar.jacoco.reportPath>${jacoco.report.path}</sonar.jacoco.reportPath>
</properties>

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.4.201502262128</version>
  <executions>
    <execution>
      <id>default-prepare-agent</id>
      <phase>process-test-classes</phase>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${jacoco.report.path}</destFile>
        <append>true</append> <!-- now appending to single jacoco.exec file -->
      </configuration>
    </execution>
  </executions>
</plugin>
当我运行mvn sonar:sonar时,我看到Jacoco传感器在所有模块上运行,但似乎只在第一个模块上运行。后续模块显示未收集覆盖率信息:

[INFO] [17:13:58.333] Sensor JaCoCoSensor
[INFO] [17:13:58.350] Analysing moduleA\target\jacoco.exec
[INFO] [17:13:58.374] No information about coverage per test.
[INFO] [17:13:58.374] Sensor JaCoCoSensor (done) | time=41ms
...
[INFO] [17:14:02.202] Sensor JaCoCoSensor
[INFO] [17:14:02.261] Analysing moduleB\moduleB1\target\jacoco.exec
[WARN] [17:14:02.334] Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?
[INFO] [17:14:02.334] Sensor JaCoCoSensor (done) | time=132ms
...
我不确定为什么在第二个和后续模块中没有覆盖信息,因为默认情况下,
maven编译器插件
包含调试信息,而且为了安全起见,我还运行了
mvn clean install-Dmaven.compiler.debug=true
,但得到了相同的结果

因此,当我在sonar服务器中检查项目时,它仅显示第一个模块的代码覆盖率:
moduleA
。不存在其他模块的代码覆盖率信息

显然,这里的解决方案是只生成一个
jacoco.exec
文件,这样当执行
jacocomaven插件时,它会将每个模块的结果附加到该文件中,以便sonar能够正确发挥其魔力

因此,我修改了我的
parentPom/pom.xml
文件,如下所示:

<properties>
  <jacoco.report.path>${project.build.directory}/jacoco.exec</jacoco.report.path>
  <sonar.jacoco.reportPath>${jacoco.report.path}</sonar.jacoco.reportPath>
</properties>

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.4.201502262128</version>
  <executions>
    <execution>
      <id>default-prepare-agent</id>
      <phase>process-test-classes</phase>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${jacoco.report.path}</destFile>
      </configuration>
    </execution>
  </executions>
</plugin>
<properties>
  <!-- single jacoco.exec file relative to root directory of the project -->
  <jacoco.report.path>${session.executionRootDirectory}/code-coverage/jacoco.exec</jacoco.report.path>
  <sonar.jacoco.reportPath>${jacoco.report.path}</sonar.jacoco.reportPath>
</properties>

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.4.201502262128</version>
  <executions>
    <execution>
      <id>default-prepare-agent</id>
      <phase>process-test-classes</phase>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${jacoco.report.path}</destFile>
        <append>true</append> <!-- now appending to single jacoco.exec file -->
      </configuration>
    </execution>
  </executions>
</plugin>
但是当我运行mvn sonar:sonar时,JaCoCoSensor似乎没有被调用,sonar服务器上的项目根本没有代码覆盖

我做错了什么?如何让sonar分析maven项目中所有模块的代码覆盖率?

我是否需要以某种方式修改
maven-surefire插件


我使用的是SonarQube 5.1、JDK 1.8、jacoco maven插件0.7.4.20150262128

jacoco传感器将仅加载您模块中涵盖的类的覆盖范围

这意味着,如果由于某种原因,模块B中的jacoco.exec不包含有关此模块的.class文件的覆盖率信息,则它不会加载任何覆盖率(即使您覆盖了其他模块中的类)