Jacoco offline instrumentation Gradle script

前端 未结 4 858
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 06:18

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

相关标签:
4条回答
  • 2020-12-06 06:29

    The answer https://stackoverflow.com/a/42238982/2689114 works for me(with some adapting to a newer gradle version).

    If you have multi-module gradle project, then you should add some extra configuration to support cross-module code coverage.

    For more details see Cross-module code coverage with Jacoco offline instrumentation in gradle mutlimodule project

    Also, there is a working example: https://github.com/SurpSG/jacoco-offline-instrumentation

    0 讨论(0)
  • 2020-12-06 06:36

    classesDir is not available in Gradle 5 this offline instrumentation code worked for me on Gradle 5.1.1

     task instrument(dependsOn: [classes, project.configurations.jacocoAnt]) {
    
     inputs.files classes.outputs.files
     File outputDir = new File(project.buildDir, 'instrumentedClasses')
     outputs.dir outputDir
     doFirst {
         project.delete(outputDir)
         ant.taskdef(
                 resource: 'org/jacoco/ant/antlib.xml',
                 classpath: project.configurations.jacocoAnt.asPath,
                 uri: 'jacoco'
         )
         def instrumented = false
         if (file(sourceSets.main.java.outputDir).exists()) {
             def instrumentedClassedDir = "${outputDir}/${sourceSets.main.java}"
             ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
                 fileset(dir: sourceSets.main.java.outputDir, includes: '**/*.class')
             }
             //Replace the classes dir in the test classpath with the instrumented one
             sourceSets.test.runtimeClasspath -= files(sourceSets.main.java.outputDir)
             sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
             instrumented = true
         }
         if (instrumented) {
             test.jvmArgs += '-noverify'
         }
     }
    }
    
    
    test.dependsOn instrument
    

    The above code is taken from the link https://github.com/esdk/g30l0/commit/82af4c9aad50aadc40d940471fe1b934473170c7 pease follow for more information.

    0 讨论(0)
  • 2020-12-06 06:38

    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')
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-06 06:42

    The gradle jacoco plugin doesn't support offline instrumentation, it always does online instrumentation via the java agent.

    If the ant jacoco plugin supports offline instrumentation that's likely the best way to get offline instrumentation working in gradle

    0 讨论(0)
提交回复
热议问题