【发布时间】:2014-02-21 22:50:55
【问题描述】:
我有一个 Jenkins Sonar 设置,带有一个 Maven 项目。这个 maven 项目只有一个 pom,但包含 java、javascript、html 等。所以对于声纳来说,它是一个多模块项目,这样我就可以得到每个部分的统计信息。
我们还想得到项目的代码覆盖率分析,这意味着我们需要在声纳分析之前运行测试并导出它们(至少从我收集的数据来看 sonar-runner 不能做这种类型的分析)。
所以我将我的 jenkins 作业设置为执行 clean package 作为第一步,然后我们运行我们添加 Invoke Standalone Sonar Analysis 作为第二步。我们在项目的根目录中有一个 sonar-project.properties 文件,其中包含所有各种设置。这里是:
sonar.projectKey=Project
sonar.projectName=Project
sonar.projectVersion=0.2.0-SNAPSHOT
sonar.projectDescription=Super Project
sonar.modules=project-java,project-web,project-js
project-java.sonar.dynamicAnalysis=reuseReports
project-java.sonar.core.codeCoveragePlugin=jacoco
project-java.sonar.dynamicAnalysis=reuseReports
project-java.sonar.junit.reportsPath=target/surefire-reports
project-java.sonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec
project-java.sonar.projectName=project-java
project-java.sonar.language=java
project-java.sonar.projectBaseDir=.
project-java.sonar.sources=src/main/java
project-java.sonar.java.source=1.7
project-web.sonar.projectName=project-web
project-web.sonar.language=web
project-web.sonar.projectBaseDir=.
project-web.sonar.sources=src/main/webapp/partials
project-js.sonar.projectName=project-js
project-js.sonar.language=js
project-js.sonar.projectBaseDir=.
project-js.sonar.sources=src/main/webapp/js
现在我在我的 pom 中添加了以下内容(大部分是从 Creating Code Coverage Reports for Unit And Integration Tests with The JaCoCo Maven Plugin 开始的):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<!--<skipTests>${skip.unit.tests}</skipTests>-->
<!-- Excludes integration tests when unit tests are run. -->
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.4.201312101107</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.build.directory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
所以当我在本地执行mvn clean package 时,一切都会运行,并且我可以转到目标目录,我会看到生成的报告(我知道声纳不使用这些,但其中包含代码覆盖率信息)。现在,当 Sonar 作业运行时,一切似乎都运行良好,但结果缺少单元测试覆盖率数字:
查看我发现的构建日志:
14:39:43.929 INFO - Sensor JaCoCoSensor...
14:39:43.932 INFO - Project coverage is set to 0% since there is no directories with classes.
14:39:43.932 INFO - Sensor JaCoCoSensor done: 3 ms
我见过“Project coverage is set to 0%” – JaCoCo and Sonar in Jenkins,并尝试了那里的建议,但无济于事。所以我假设我错过了另一个设置或设置错误。
【问题讨论】: