本文介绍了gradle mutlimodule项目中使用Jacoco离线工具进行跨模块代码覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在项目中使用 Jacoco离线工具,因为还使用了PowerMock.

I have to use Jacoco offline instrumentation in my project because there is PowerMock that is used as well.

问题描述:假设您有一个带有两个模块的gradle项目:A,B.模块A的测试涵盖了来自模块B的代码.在代码覆盖率数据收集中,我发现完全丢失了模块B的覆盖率数据(应由模块A提供).

The issue description: suppose you have gradle project with two modules: A, B. Module A has tests that cover a code from the module B. On code coverage data collection I figured out that coverage data(should be provided by the module A) for the module B is completely missed.

我创建了一个演示该问题的测试项目: https://github.com /SurpSG/jacoco-offline-instrumentation

I've created a test project that demonstrates the issue: https://github.com/SurpSG/jacoco-offline-instrumentation

针对gradle项目的Jacoco离线检测设置基于答案 https://stackoverflow.com/a/42238982/2689114

Jacoco offline instrumentation setup for gradle project is based on the answer https://stackoverflow.com/a/42238982/2689114

另一方面,当我使用jacoco gradle插件时,我可以观察到模块A为模块B提供的覆盖率数据已成功收集到摘要报告中.我创建了另一个测试项目来证明这一点: https://github. com/SurpSG/jacoco-gradle-plugin-merge-coverage

On the other hand, when I'm using jacoco gradle plugin I can observe that coverage data provided by module A for module B successfully collected to a summary report. I've created one more test project to demonstrate this: https://github.com/SurpSG/jacoco-gradle-plugin-merge-coverage

我是否为gradle多模块项目+ jacoco脱机检测设置错误?

Am I have a wrong setup for the gradle multimodule project + jacoco offline instrumentation?

推荐答案

经过一番调查,我发现Gradle中的模块依赖关系是通过.jar文件解决的:

After some investigation, I figured out that modules dependencies in Gradle are resolved via .jar files:

<dependent-module>.classpath contains <dependency-module>.jar

因此,就我而言,我需要构建一些包含检测类的特殊jar.

So, in my case, I need to build some special jar that contains instrumented classes.

乐器类

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)
            }
        }
    }

下一步将是制造仪器罐:

task jacocoInstrumentedJar(type: Jar, dependsOn: [preprocessClassesForJacoco]) {
    baseName "${project.name}-instrumented"
    from preprocessClassesForJacoco.outputDir // path to instrumented classes
}

最后,我们需要替换普通的.jar ,并使用已安装的一个

And finally, we need to replace the usual .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-具有上述步骤的离线仪器.随时检查该项目以尝试.

I've updated the example project https://github.com/SurpSG/jacoco-offline-instrumentation with steps described above. Feel free to check out the project to try.

这篇关于gradle mutlimodule项目中使用Jacoco离线工具进行跨模块代码覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 18:23