当前位置: 首页 > 知识库问答 >
问题:

Jacoco离线检测Gradle脚本

越宣
2023-03-14

我尝试寻找Jacoco离线检测gradle脚本片段,但找不到一个。是否可以通过gradle脚本进行Jacoco离线检测?如果是的话…一个很好的例子。谢谢。

共有3个答案

闻安宜
2023-03-14

答案https://stackoverflow.com/a/42238982/2689114这对我很有用(有些是为了适应更新的gradle版本)。

如果您有多模块gradle项目,那么您应该添加一些额外的配置来支持跨模块代码覆盖。

有关更多详细信息,请参阅gradle Mutlimod项目中Jacoco离线检测的跨模块代码覆盖率

还有一个工作示例:https://github.com/SurpSG/jacoco-offline-instrumentation

UPD:有另一种在离线模式下使用jacoco的jacoco gradle插件

梁承恩
2023-03-14

ClassesDir在Gradle 5中不可用,这个离线检测代码在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

上面的代码取自链接 https://github.com/esdk/g30l0/commit/82af4c9aad50aadc40d940471fe1b934473170c7 关注以获取更多信息。

丌官皓君
2023-03-14

以下是使用JaCoCo Ant任务执行离线检测的Gradle脚本示例:

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')
    }
  }
}
 类似资料:
  • 开发离线应用的第一步是要知道设备是在线还是离线,HTML5为此定义了一个navigator.onLine属性,这个属性值为true 表示设备能上网,值为false 表示设备离线。这个属性的关键是浏览器必须知道设备能否访问网络,从而返回正确的值。实际应用中,navigator.onLine 在不同浏览器间还有些小的差异。 IE6+和Safari 5+能够正确检测到网络已断开,并将navigator.

  • 在渲染器进程中,使用标准HTML5 API实现在线和离线事件检测,例子: main.js 1 const {app, BrowserWindow} = require('electron') 2 ​ 3 let onlineStatusWindow 4 ​ 5 app.on('ready', () => { 6 onlineStatusWindow = new BrowserWindow({ wi

  • 问题内容: 如何在JavaScript中检测Internet连接是否离线? 问题答案: 您可以通过发出 失败的XHR请求 来确定连接丢失。 标准方法是 重试 几次 请求 。如果没有通过,请 警告用户 检查连接,然后 正常失败 。 旁注: 将整个应用程序置于“脱机”状态可能会导致很多容易出错的状态处理工作。无线连接可能会来去去等等。因此,最好的选择是妥善地失败,保留数据,并警告用户..允许他们最终解

  • 问题内容: 如何在JavaScript中检测Internet连接是否离线? 问题答案: 您可以通过发出 失败的XHR请求 来确定连接丢失。 标准方法是 重试 几次 请求 。如果未通过,请 警告用户 检查连接,然后 正常失败 。 旁注: 将整个应用程序置于“脱机”状态可能会导致很多容易出错的状态处理工作。无线连接可能会来去去等等。因此,最好的选择是妥善地失败,保留数据,并警告用户..允许他们最终解决

  • 我正在使用Spring靴与尤里卡,它的工作真的很好。但几个小时后,我想更快地检测离线Eureka实例/客户端,但我没有找到关于Eureka配置属性的好文档。我甚至不确定这是否可能,因为Eureka似乎假定客户每30秒发送一次更新。 我开始停用自我保护模式,提高更新和间隔更新的速度,降低过期时间,但我的Eureka服务器仍然需要两分钟来发现它的离线客户端。

  • 我一直在尝试在JBoss服务器中实现JaCoCo离线代码覆盖,使用仪表化的EAR进行部署和jacococagent.jar,以便跟踪针对所述JBoss运行的外部集成测试的代码覆盖。 我一直在关注这样的指南: http://www.eclemma.org/jacoco/trunk/doc/offline.html http://automationrhapsody.com/code-coverage