gradle 多模块项目中 Jacoco 离线检测的跨模块代码覆盖率
Cross-module code coverage with Jacoco offline instrumentation in gradle mutlimodule project
我必须在我的项目中使用 Jacoco 离线工具,因为还使用了 PowerMock。
问题描述:假设您的 gradle 项目包含两个模块:A、B。模块 A
的测试覆盖了模块 B
中的代码。在代码覆盖率数据收集上,我发现模块 B
的覆盖率数据(应该由模块 A
提供)完全丢失了。
我创建了一个演示该问题的测试项目:https://github.com/SurpSG/jacoco-offline-instrumentation
gradle 项目的 Jacoco 离线检测设置基于答案
另一方面,当我使用 jacoco gradle 插件时,我可以观察到模块 A
为模块 B
提供的覆盖数据已成功收集到摘要报告中。我又创建了一个测试项目来证明这一点:https://github.com/SurpSG/jacoco-gradle-plugin-merge-coverage
我对 gradle 多模块项目 + jacoco 离线检测的设置有误吗?
经过一些调查,我发现 Gradle 中的模块依赖关系是通过 .jar 文件解决的:
<dependent-module>.classpath contains <dependency-module>.jar
因此,就我而言,我需要构建一些特殊的 jar,其中包含经过检测的 类。
检测类
task preprocessClassesForJacoco(dependsOn: ['classes']) {
ext.outputDir = buildDir.path + '/classes-instrumented'
doLast {
ant.taskdef(name: 'instrument',
classname: 'org.jacoco.ant.InstrumentTask',
classpath: configurations.jacoco.asPath)
ant.instrument(destdir: outputDir) {
fileset(dir: sourceSets.main.java.outputDir, includes: '**/*.class', erroronmissingdir: false)
}
}
}
下一步将是构建检测 jar:
task jacocoInstrumentedJar(type: Jar, dependsOn: [preprocessClassesForJacoco]) {
baseName "${project.name}-instrumented"
from preprocessClassesForJacoco.outputDir // path to instrumented classes
}
最后,我们需要 替换 常用的 .jar with instrumented one
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(preprocessClassesForJacoco)) {
tasks.withType(Test) {
doFirst {
...
// getting a module dependencies
def modulesDependencies = moduleDependencies(project)
// removing regular jars
classpath -= files(modulesDependencies.jar.outputs.files)
// adding instrumented jars
classpath += files(modulesDependencies.jacocoInstrumentedJar.outputs.files)
}
}
}
}
我已经使用上述步骤更新了示例项目 https://github.com/SurpSG/jacoco-offline-instrumentation。请随时查看该项目进行尝试。
我必须在我的项目中使用 Jacoco 离线工具,因为还使用了 PowerMock。
问题描述:假设您的 gradle 项目包含两个模块:A、B。模块 A
的测试覆盖了模块 B
中的代码。在代码覆盖率数据收集上,我发现模块 B
的覆盖率数据(应该由模块 A
提供)完全丢失了。
我创建了一个演示该问题的测试项目:https://github.com/SurpSG/jacoco-offline-instrumentation
gradle 项目的 Jacoco 离线检测设置基于答案
另一方面,当我使用 jacoco gradle 插件时,我可以观察到模块 A
为模块 B
提供的覆盖数据已成功收集到摘要报告中。我又创建了一个测试项目来证明这一点:https://github.com/SurpSG/jacoco-gradle-plugin-merge-coverage
我对 gradle 多模块项目 + jacoco 离线检测的设置有误吗?
经过一些调查,我发现 Gradle 中的模块依赖关系是通过 .jar 文件解决的:
<dependent-module>.classpath contains <dependency-module>.jar
因此,就我而言,我需要构建一些特殊的 jar,其中包含经过检测的 类。
检测类
task preprocessClassesForJacoco(dependsOn: ['classes']) {
ext.outputDir = buildDir.path + '/classes-instrumented'
doLast {
ant.taskdef(name: 'instrument',
classname: 'org.jacoco.ant.InstrumentTask',
classpath: configurations.jacoco.asPath)
ant.instrument(destdir: outputDir) {
fileset(dir: sourceSets.main.java.outputDir, includes: '**/*.class', erroronmissingdir: false)
}
}
}
下一步将是构建检测 jar:
task jacocoInstrumentedJar(type: Jar, dependsOn: [preprocessClassesForJacoco]) {
baseName "${project.name}-instrumented"
from preprocessClassesForJacoco.outputDir // path to instrumented classes
}
最后,我们需要 替换 常用的 .jar with instrumented one
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(preprocessClassesForJacoco)) {
tasks.withType(Test) {
doFirst {
...
// getting a module dependencies
def modulesDependencies = moduleDependencies(project)
// removing regular jars
classpath -= files(modulesDependencies.jar.outputs.files)
// adding instrumented jars
classpath += files(modulesDependencies.jacocoInstrumentedJar.outputs.files)
}
}
}
}
我已经使用上述步骤更新了示例项目 https://github.com/SurpSG/jacoco-offline-instrumentation。请随时查看该项目进行尝试。