简体   繁体   English

排除不适用于 Jacoco Gradle 插件

[英]Exclusions not working with Jacoco Gradle plugin

I'm working on setting up the Jacoco Gradle plugin for my project.我正在为我的项目设置 Jacoco Gradle 插件。 The plugin is executing fine and generating the coverage report but it's not excluding packages that I would excluded.该插件运行良好并生成覆盖率报告,但它不排除我会排除的包。

Classes I would like to include are in com/xxx/ws/hn/** and classes I would like excluded are in com/xxx/ws/enterprise/**.我想包含的类在 com/xxx/ws/hn/** 中,我想排除的类在 com/xxx/ws/enterprise/** 中。

Here's my Gradle script:这是我的 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
    }
}

Am I missing anything here?我在这里错过了什么吗? I've tried various patterns of exclusions including a '.'我尝试了各种排除模式,包括“。” delimiter for packages instead of a '/' but nothing seems to work.包的分隔符而不是“/”,但似乎没有任何作用。 Any help would be greatly appreciated.任何帮助将不胜感激。

I have not verified this but when I read the JaCoCo documentation, it states that wildcards can be used but all examples only have ONE *, not two as your example shows.我尚未验证这一点,但是当我阅读 JaCoCo 文档时,它指出可以使用通配符,但所有示例只有一个 *,而不是您的示例所示的两个。

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

To solidify (and verify) Joachim's answer, I came across this SO post and found that specifying canonical class names, classpath style (with slashes, not periods), with single wildcard characters actually worked well.为了巩固(并验证)Joachim 的答案,我看到了这篇 SO 帖子,发现使用单个通配符指定规范类名、类路径样式(带斜杠,而不是句点)实际上效果很好。 For example, here is my exclude list that works in my build, as defined as a list in a separate gradle file:例如,这是在我的构建中使用的排除列表,定义为单独的 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'
]

I tried probably 10 or 12 other ways of formatting the class names, and this is the only way that actually worked.我尝试了大约 10 或 12 种其他方式来格式化类名,这是唯一有效的方式。 I don't know why something like this that is so simple/fundamental is so ungoogleable and difficult to find a definitive answer on in the context of Gradle (ie outside JaCoCo's official documentation, where no documentation exists about Gradle integration, but Ant and Maven integration docs are there).我不知道为什么像这样简单/基本的东西如此难以搜索,并且很难在 Gradle 的上下文中找到明确的答案(即在 JaCoCo 的官方文档之外,其中没有关于 Gradle 集成的文档,但是 Ant 和 Maven集成文档在那里)。

Hopefully this helps if others are flailing trying to get JaCoCo to exclude classes.如果其他人试图让 JaCoCo 排除类,希望这会有所帮助。

UPDATE: This changed in Gradle 7 (I went from 5.6.1 to 7.1.1, so not sure about Gradle 6) and no longer works.更新:这在 Gradle 7 中发生了变化(我从 5.6.1 到 7.1.1,所以不确定 Gradle 6)并且不再有效。 For Gradle 7 you need to replace / with .;对于 Gradle 7,您需要将 / 替换为 .; here are my updated exclusions:这是我更新的排除项:

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'
]

Also see the following SO post for other means of excluding classes from reporting (exclude from reporting, not coverage itself):另请参阅以下 SO 帖子,了解从报告中排除类的其他方法(从报告中排除,而不是覆盖本身):

Filter JaCoCo coverage reports with Gradle使用 Gradle 过滤 JaCoCo 覆盖率报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM