SonarQube:使用 JaCoCo 的多模块 gradle 项目的覆盖范围不完整 [英] SonarQube: Coverage incomplete on multimodule gradle project with JaCoCo
问题描述
我正在构建一个 SonarQube 6.2 服务器,它已经在分析我的 Java 8/Gradle 3.3 项目.在将 JaCoCo 添加到多模块 gradle 项目时,我意识到 SonarQube 正在基于每个模块"测量代码覆盖率:
I am building a SonarQube 6.2 server which is already analyzing my Java 8/Gradle 3.3 projects. When adding JaCoCo to a multimodule gradle project, I realized that SonarQube is measuring code coverage on a "per-module" basis:
如果一个类位于模块 A
中,并且该类的测试位于模块 B
中,SonarQube 会认为该类未被覆盖.
If a class is located in module A
and a test for this class is located in module B
, SonarQube figures the class is not covered.
我想测量所有模块的代码覆盖率,而不是基于每个模块.我如何做到这一点?
I want to measure code coverage across all modules, not on a per module basis. How do I achieve this?
有很多类似的问题,但没有有用的答案,尽管这种情况对我来说似乎很常见.例如,Jenkins 默认会这样做.
There are lots of similar questions but no helpful answers, although the situation seems quite common to me. Jenkins for example does that per default.
我决定在 github 上构建一个 蓝图 来澄清这个问题.
I decided to build a blueprint on github to clarify the issue.
主要的build.gradle
由
plugins { id "org.sonarqube" version "2.2.1" }
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
repositories { mavenCentral() }
dependencies { testCompile "junit:junit:4.12" }
}
modA/build.gradle
为空.
它包含3个类:TestedInModA
、TestedInModATest
和TestedViaModB
.
modB/build.gradle
只声明对modA
的依赖:
dependencies { compile project(':modA') }
它只包含一个类:TestedViaModBTest
,测试位于modA
中的类TestedViaModB
.
It contains just one class: TestedViaModBTest
, testing the class TestedViaModB
located in modA
.
我的(私有)Jenkins 实例显示 100% 覆盖了所包含的两个类,而 SonarQube 表示只覆盖了类 TestedInModA
(在其自己的模块中进行了测试).
My (private) Jenkins instance shows 100% coverage for the two classes included while SonarQube says only the class TestedInModA
(which is tested in its own module) is covered.
如何修改我的构建过程以在 SonarQube 中查看跨模块覆盖"?
How can I modify my build process to see "cross-module coverage" in SonarQube?
我很想更新我的项目,以便未来访问此问题的人可以找到一个可行的示例.
I would love to update my project so future visitors to this question can find a working example.
我的工作解决方案(感谢@Godin)
将以下内容添加到
subprojects
闭包
tasks.withType(Test) {
// redirect all coverage data to one file
// ... needs cleaning the data prior to the build to avoid accumulating coverage data of different runs.
// see `task cleanJacoco`
jacoco {
destinationFile = file("$rootProject.buildDir/jacoco/test.exec")
}
}
添加
add
task cleanJacoco(dependsOn: 'clean') { delete "$buildDir/jacoco" }
在 subprojects
闭包之外.
推荐答案
当你执行构建时,JaCoCo Gradle Plugin 会产生 modA/build/jacoco/test.exec
和 modB/build/jacoco/test.exe
分别包含有关在 modA
和 modB
中执行测试的信息.SonarQube 单独执行模块分析,因此在分析文件 TestedViaModB
的 modA
期间,它只看到 modA/build/jacoco/test.exec
.
When you perform build JaCoCo Gradle Plugin will produce modA/build/jacoco/test.exec
and modB/build/jacoco/test.exe
that contain information about execution of tests in modA
and modB
respectively. SonarQube performs analysis of modules separately, so during analysis of modA
for the file TestedViaModB
it sees only modA/build/jacoco/test.exec
.
最常见的跨越边界的技巧 - 是将所有覆盖信息收集到一个位置.这可以通过JaCoCo Gralde 插件
Most common trick to cross boundaries - is to collect all coverage information into single location. This can be done with JaCoCo Gralde Plugin
通过更改位置 - 请参阅
destinationFile
和destPath
(因为信息被附加到exec
文件,不要忘记在构建之前删除这个单个位置,否则它将累积来自不同构建的信息,而不仅仅是来自不同模块的信息),
either by changing location - see
destinationFile
anddestPath
( since information is appended toexec
file, don't forget to remove this single location prior to build, otherwise it will be accumulating information from different builds and not just from different modules ),
将所有文件合并为一个文件 - 请参阅 JacocoMerge
任务.然后将 SonarQube 的这个单一位置指定为 sonar.jacoco.reportPath
.
either by merging all files into single one - see JacocoMerge
task. And then specify this single location to SonarQube as sonar.jacoco.reportPath
.
另一个技巧:SonarQube 6.2 with Java Plugin 4.4 支持属性 sonar.jacoco.reportPaths
允许指定多个位置.
Another trick: SonarQube 6.2 with Java Plugin 4.4 supports property sonar.jacoco.reportPaths
allowing to specify multiple locations.
这篇关于SonarQube:使用 JaCoCo 的多模块 gradle 项目的覆盖范围不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!