我有一個Jenkins Sonar設置,帶有一個maven項目。這個maven項目有一個單獨的pom,但是包含java,javascript,html等。因此對於聲納它是一個多模塊項目,這樣我就可以得到每個部分的統計數據。缺少單元測試覆蓋率,但有單元測試統計
我們也想獲得項目的代碼覆蓋率分析,這意味着我們需要在聲納分析之前運行測試並導出它們(至少從我收集的聲吶運行器不能做到這種類型曲線變化分析)。
所以,我有我的工作詹金斯成立做clean package
作爲第一步,然後我們跑,我們添加調用獨立的聲納分析作爲第二步。我們在項目的根目錄中有一個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
當地一切都正常運行,我可以去到目標目錄,我看到生成的報告(我知道聲納不使用這些,但它有代碼覆蓋信息)。現在,當聲納作業運行一切似乎都做工精細,但結果是缺少單元測試覆蓋率的數字:
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,並試圖在那裏的建議無濟於事。所以我假設我錯過了另一個設置或者設置了錯誤。
我們必須在大約同一時間發佈。無論如何,事實證明我錯過了一個屬性,它需要告訴jacoco編譯的類文件位於何處。不知何故錯過了這一點,或者在我之前經歷過的所有例子中都假設了知識。 (還是)感謝你的建議。 –