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
的依赖关系:
modB/build.gradle
just declares a dependency to 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插件将生成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
.
另一个技巧:带有Java Plugin 4.4的SonarQube 6.2支持属性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屋!