Jacoco离线工具Gradle脚本 [英] Jacoco offline instrumentation Gradle script

查看:839
本文介绍了Jacoco离线工具Gradle脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找Jacoco离线工具gradle脚本片段,但找不到一个。是否有可能通过gradle脚本执行Jacoco离线检测?如果是的话......一个例子就是伟大的。感谢。

I tried looking for Jacoco offline instrumentation gradle script snippets but couldn't find one. Is it possible to do Jacoco offline instrumentation through gradle scripts ? If yes...An example of it would be greats. Thanks.

推荐答案

这是一个使用JaCoCo Ant Task执行离线检测的Gradle脚本示例:

Here is an example of Gradle script that performs offline instrumentation using JaCoCo Ant Task:

apply plugin: 'java'

configurations {
  jacoco
  jacocoRuntime
}

dependencies {
  jacoco group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.7.9', classifier: 'nodeps'
  jacocoRuntime group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.7.9', classifier: 'runtime'
  testCompile 'junit:junit:4.12'
}

repositories {
  mavenCentral()
}

task instrument(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.output.classesDir)
    }
  }
}

gradle.taskGraph.whenReady { graph ->
  if (graph.hasTask(instrument)) {
    tasks.withType(Test) {
      doFirst {
        systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/tests.exec'
        classpath = files(instrument.outputDir) + classpath + configurations.jacocoRuntime
      }
    }
  }
}

task report(dependsOn: ['instrument', 'test']) {
  doLast {
    ant.taskdef(name: 'report',
                classname: 'org.jacoco.ant.ReportTask',
                classpath: configurations.jacoco.asPath)
    ant.report() {
      executiondata {
        ant.file(file: buildDir.path + '/jacoco/tests.exec')
      }
      structure(name: 'Example') {
         classfiles {
           fileset(dir: sourceSets.main.output.classesDir)
         }
         sourcefiles {
           fileset(dir: 'src/main/java')
         }
      }
      html(destdir: buildDir.path + '/reports/jacoco')
    }
  }
}

这篇关于Jacoco离线工具Gradle脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆