【问题标题】:Exclusions not working with Jacoco Gradle plugin排除不适用于 Jacoco Gradle 插件
【发布时间】:2013-11-16 11:47:40
【问题描述】:

我正在为我的项目设置 Jacoco Gradle 插件。该插件执行良好并生成覆盖率报告,但它不排除我将排除的包。

我想包含的课程在 com/xxx/ws/hn/** 中,我想排除的课程在 com/xxx/ws/enterprise/** 中。

这是我的 Gradle 脚本:

apply plugin: "jacoco"

jacoco {
    toolVersion = "0.6.2.201302030002"
    reportsDir = file("$buildDir/reports/jacoco")
}

test {
    jacoco{
        excludes = ["com/xx/ws/enterprise/**/*"]
        includes = ["com/xxx/ws/hn/**/*"]
        append = false
    }
}

jacocoTestReport {
    dependsOn test
    description = "Generate Jacoco coverage reports after running tests."
    executionData = files('build/jacoco/test.exec')
    reports {
        xml.enabled true
        html.enabled true
    }
}

我在这里遗漏了什么吗?我尝试了各种排除模式,包括“。”包的分隔符而不是“/”,但似乎没有任何效果。任何帮助将不胜感激。

【问题讨论】:

    标签: java gradle jacoco


    【解决方案1】:

    我尚未对此进行验证,但是当我阅读 JaCoCo 文档时,它指出可以使用通配符,但所有示例都只有一个 *,而不是您的示例显示的两个。

    http://www.eclemma.org/jacoco/trunk/doc/agent.html

    【讨论】:

      【解决方案2】:

      为了巩固(和验证)Joachim 的回答,我遇到了这篇 SO 帖子,发现使用单个通配符指定规范类名、类路径样式(带有斜线,而不是句点)实际上效果很好。例如,这是在我的构建中工作的排除列表,定义为单独的 gradle 文件中的列表:

      def testExcl = [
          'android/*',
          'com/android/*',
          'com/nativelibs4java/*',
          'com/ochafik/*',
          'com/fasterxml/*',
          'com/esotericsoftware/*',
          'com/google/*',
          'com/lmax/*',
          'com/sun/*',
          'jdk/*',
          'mockit/*',
          'org/apache/*',
          'org/bridj/*',
          'org/gradle/*',
          'org/hamcrest/*',
          'org/junit/*',
          'org/slf4j/*',
          'sun/*',
          'worker/*',
          '*Test',
          '*TestIntegration',
          '*AllTests',
          '*Suite'
      ]
      

      我尝试了大约 10 或 12 种其他格式化类名的方法,这是唯一有效的方法。 我不知道为什么像这样简单/基本的东西在 Gradle 的上下文中如此难以用谷歌搜索并且很难找到明确的答案(即在 JaCoCo 的官方文档之外,其中没有关于 Gradle 集成的文档,但是 Ant 和 Maven集成文档在那里)。

      如果其他人试图让 JaCoCo 排除类,希望这会有所帮助。

      更新: 这在 Gradle 7 中发生了变化(我从 5.6.1 升级到 7.1.1,所以不确定 Gradle 6)并且不再有效。 对于 Gradle 7,您需要将 / 替换为 .;这是我更新的排除项:

      def testExcl = [
          'android.*',
          'com.android.*',
          'com.nativelibs4java.*',
          'com.ochafik.*',
          'com.fasterxml.*',
          'com.esotericsoftware.*',
          'com.google.*',
          'com.lmax.*',
          'com.sun.*',
          'jdk.*',
          'mockit.*',
          'org.apache.*',
          'org.bridj.*',
          'org.gcpud.common.*',
          'org.gcpud.testutil.*',
          'org.gradle.*',
          'org.hamcrest.*',
          'org.junit.*',
          'org.slf4j.*',
          'sun.*',
          'worker.*',
          '*Test',
          '*Test$*',
          '*TestIntegration',
          '*AllTests',
          '*Suite'
      ]
      

      另请参阅以下 SO 帖子,了解从报告中排除课程的其他方式(从报告中排除,而不是覆盖范围本身):

      Filter JaCoCo coverage reports with Gradle

      【讨论】:

        猜你喜欢
        • 2014-07-08
        • 2014-10-11
        • 2022-01-10
        • 2016-08-19
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        • 2019-01-04
        • 2014-03-22
        相关资源
        最近更新 更多