【发布时间】:2015-04-19 19:38:57
【问题描述】:
我们有一个多模块多语言 maven java 项目,使用 jacoco 进行覆盖率分析。 模块的主要部分是带有 REST API 的后端(Java 代码),我们的 webapp 模块包含前端(AngularJS)和 java 中的集成测试。 我们的 Jacoco-IT.exec 文件包含大约 100Kb 的数据,因此我们猜测可能会收集一些用于集成测试的 Coverage 数据。尽管如此,我们在 SonarQube 中看不到任何覆盖(使用版本 5.0 和 4.5)
我们运行构建项目并运行集成测试
mvn clean install
分析数据
mvn sonar:sonar
项目配置为多语言,但我们只是分析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>
我们来自 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>
您可以在我们的父 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
是“没有关于每次测试覆盖率的信息”。一个问题?好的,我们不知道是哪个测试导致了覆盖率,但我们应该在 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
这应该不是问题,因为此模块中没有类。 这个观察正确吗?
在我们的配置基于官方 SonarCube 演示配置 (https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/combined%20ut-it/combined-ut-it-multimodule-maven-jacoco) 的主要部分中,我们刚刚添加了“prepare-agent-integration”目标。当然,也将其更改为多语言项目。
我们在网络上发现了很多被贬低的信息(argLine 设置等),这些信息对我们没有任何改变。
我们使用的文档和教程的链接:
【问题讨论】:
-
你的类文件在哪里? Maven 使用的默认位置并不总是由所有插件输出目录映射。我不确定,但我猜 maven 使用“/target”,但有些插件可能使用“/bin”(或相反)。为避免这种情况,您可以设置
-
这些类都在目标/类中,除了刚刚测试的 webapp。这些类在目标/测试类中。
-
在声纳分析运行期间,它还会打印出二进制文件的正确路径。
-
您可以尝试在
<build>标签中设置以下两项:<sourceDirectory>src/main</sourceDirectory>和<outputDirectory>target</outputDirectory>。运行测试。如果它不起作用,请删除<properties>标签及其内容并重新运行。 -
我现在将我的父 POM 更改为:
<build> <sourceDirectory>src/main</sourceDirectory> <outputDirectory>target</outputDirectory> <plugins>...但我能注意到的唯一区别是 webapp 模块(运行 IT 并且没有 java 类)的消息现在有以下消息分析(mvn sonar:sonar):Analysing target\jacoco-it.exec Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?之前:No JaCoCo analysis of project coverage can be done since there is no class files.
标签: maven configuration sonarqube integration-testing jacoco-maven-plugin