如何获得 scala 项目的 SonarQube 代码覆盖率
Posted
技术标签:
【中文标题】如何获得 scala 项目的 SonarQube 代码覆盖率【英文标题】:How to get SonarQube code coverage for scala project 【发布时间】:2021-11-17 18:55:19 【问题描述】:我正在运行 Teamcity for Scala 项目的目标
org.jacoco:jacoco-maven-plugin:prepare-agent -U sonar:sonar
我的 pom.xml 文件如下所示:
<sonar-maven-plugin.version>2.0</sonar-maven-plugin.version>
<!-- Sonar -->
<jacoco.version>0.7.9</jacoco.version>
<sonar.projectName>ds-rfds</sonar.projectName>
<sonar.projectDescription>ds-rfds</sonar.projectDescription>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.scala.coverage.reportPaths>$project.basedir/ds-rfds/target</sonar.scala.coverage.reportPaths>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.language>scala</sonar.language>
<junit.version>4.11</junit.version>
我无法得到报告,它给出的错误如下:
Sensor Scoverage sensor for Scala coverage [sonarscala]
18:34:58
[INFO] Importing coverage from /mrit_cm3/apps/teamcity/7.1.3/buildagent1/work/84f58bcce55c6907/ds-rfds/target
18:34:58
[ERROR] File '/mrit_cm3/apps/teamcity/7.1.3/buildagent1/work/84f58bcce55c6907/ds-rfds/target' can't be read. java.io.FileNotFoundException: /mrit_cm3/apps/teamcity/7.1.3/buildagent1/work/84f58bcce55c6907/ds-rfds/target (No such file or directory)
18:34:58
java.io.FileNotFoundException: /mrit_cm3/apps/teamcity/7.1.3/buildagent1/work/84f58bcce55c6907/ds-rfds/target (No such file or directory)
我应该如何解决这个问题? 我正在使用 SonarQube Enterprise EditionVersion 7.9.1
【问题讨论】:
你需要jacoco:report或jacoco-report-aggregate 【参考方案1】:SonarQube 不会创建代码覆盖率报告,它只是读取它。我怀疑您缺少将成为板条箱报告的命令。它应该是这样的:
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<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>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
注意这个<execution><id>post-unit-test</id>
部分很重要,因为这里是创建文件的地方。
此外,您需要在 sonarqube 属性中设置此路径(路径:<SONAR_QUBE_INSTALL_FOLDER>\conf\sonar.properties
)并添加以下内容:
sonar.junit.reportsPath=target/surefire-reports
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/coverage-reports/jacoco-unit.exec
sonar.binaries=target/classes
sonar.java.coveragePlugin=jacoco
sonar.verbose=true
【讨论】:
以上是关于如何获得 scala 项目的 SonarQube 代码覆盖率的主要内容,如果未能解决你的问题,请参考以下文章