Gradle 如何使用一些排除的类和jacoco插件验证最小覆盖率?
我需要检查新jacoco任务的最低覆盖率 jacocotestcoverage验证 此任务在3.4.1 gradle版本和jacoco插件>=0.6.3中提供 我可以运行另一个任务,生成一个包含分支覆盖率的html报告,但现在我想使用这个数字使构建失败 这是我的密码Gradle 如何使用一些排除的类和jacoco插件验证最小覆盖率?,gradle,jacoco,Gradle,Jacoco,我需要检查新jacoco任务的最低覆盖率 jacocotestcoverage验证 此任务在3.4.1 gradle版本和jacoco插件>=0.6.3中提供 我可以运行另一个任务,生成一个包含分支覆盖率的html报告,但现在我想使用这个数字使构建失败 这是我的密码 buildscript { ext { .... } repositories { mavenCentral() maven { ...
buildscript {
ext {
....
}
repositories {
mavenCentral()
maven {
....
}
}
dependencies {
.....
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'
jar {
baseName = "coverage-test"
}
dependencies {
// my dependencies
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
wrapper {
gradleVersion = '3.4.1'
}
jacoco {
toolVersion = '0.7.9'
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
}
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(
dir: it,
excludes:
[
'com/jacoco/dto/**',
'com/jacoco/configs/**',
//and others
])
})
}
}
jacocoTestCoverageVerification {
//I tried this and it didn't work
// classDirectories = files(classDirectories.files.collect {
// fileTree(
// dir: it,
// excludes:
// [
// 'com/jacoco/dto/**',
// 'com/jacoco/configs/**',
// //and others
// ])
// })
violationRules {
rule {
//Also tried this and it didn't work
// excludes = ['com/jacoco/dto/**', ...]
limit {
counter = 'BRANCH'
minimum = 0.8
}
}
}
}
check.dependsOn jacocoTestCoverageVerification
使用classDirectories时,我遇到以下错误:无法获取null对象上的属性“files”。使用第二个选项(仅排除),构建运行平稳,但不排除任何类。您正在测量排除的另一个对象。默认的JaCoCo范围是“BUNDLE”,我相信这意味着整个代码。我从来没用过。我总是只测量“类”范围。看起来你也在尝试做同样的事情 排除是相对于范围中的元素的。不确定它对“BUNDLE”意味着什么,但我几乎倾向于认为它要么是全部,要么是全部。此外,排除使用不同类型的通配符。尝试更改配置以使用元素“类”(或“包”)
在我的例子中,我确实想使用BUNDLE作用域为整个设置一个阈值,同时排除某些包和文件 最后对我有效的是添加了
classDirectories
exclude,正如原始问题中所建议的,但是在afterEvaluate
中,如下所示:
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'com/example/my/package/*',
'com/example/service/MyApplication.class',
'com/google/protobuf/*'
])
})
}
apply plugin: "jacoco”
jacocoTestCoverageVerification {
afterEvaluate {
getClassDirectories().setFrom(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'com/example/my/package/*',
'com/example/service/MyApplication.class',
'com/google/protobuf/*'
])
})
}
violationRules {
rule {
limit {
minimum = 0.79
}
}
}
}
// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification
作为参考,完整的build.gradle
如下所示:
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'com/example/my/package/*',
'com/example/service/MyApplication.class',
'com/google/protobuf/*'
])
})
}
apply plugin: "jacoco”
jacocoTestCoverageVerification {
afterEvaluate {
getClassDirectories().setFrom(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'com/example/my/package/*',
'com/example/service/MyApplication.class',
'com/google/protobuf/*'
])
})
}
violationRules {
rule {
limit {
minimum = 0.79
}
}
}
}
// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification
你可以在我的博客中找到更多细节:顺便说一句,这是我在项目中尝试的要点(它成功了):这就是项目:是的,你是对的,我试图衡量两种不同的东西。此外,我还尝试使用您的配置添加一个检查规则,它可以正常工作。谢谢这太完美了。我一直试图在规则中使用排除,但没有看到任何变化。谢谢你弄明白了。投赞成票!至少在Gradle 6中,此操作失败,原因如下:
无法为任务设置只读属性“classDirectories”:类型为org.Gradle.testing.jacoco.tasks.jacocococoverage的jacococoverification。
@tschumann replace classDirectories=files(classDirectories.files.collect TO getClassDirectories().setFrom(classDirectories.files)。collect@DaniilVolkov是我的英雄,谢谢!