鉴于使用 Gradle 5.0 构建的多模块项目,我想使用jacoco强制执行所有代码覆盖。 我可以在模块级别设置coverage强制执行。但是由于依赖关系的性质,许多测试是在集成模块中编写的,该模块涵盖了大多数代码。因此,仅当整体代码覆盖率低于特定阈值时,我才想使构建 失败。 以下是我的build.gradle,无论我为累积覆盖率设置什么值,构建都不会失败。
apply plugin: 'java'
apply plugin: 'jacoco'
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
tasks.withType(JacocoReport).all {
reports {
html.enabled true
}
check.dependsOn it
}
jacocoTestReport {
executionData = fileTree("$buildDir/jacoco/test.exec")
subprojects.each {
sourceSets it.sourceSets.main
dependsOn it.test
}
}
jacocoTestCoverageVerification {
executionData fileTree("$buildDir/jacoco/test.exec")
violationRules {
rule {
limit {
counter = 'LINE'
minimum = 0.5 //individual module coverage level
}
failOnViolation true
}
}
}
tasks.build.dependsOn(jacocoTestReport)
tasks.build.dependsOn(jacocoTestCoverageVerification)
jacocoTestReport.mustRunAfter jacocoTestCoverageVerification
check.dependsOn jacocoTestCoverageVerification
}
//overall cumulative coverage report
task jacocoMergeTestReport(type: JacocoReport) {
executionData fileTree(project.rootDir.absolutePath).include('*/build/jacoco/*.exec')
subprojects.each {
sourceSets it.sourceSets.main
dependsOn it.test
}
reports {
html {
enabled true
destination rootProject.file("$rootProject.buildDir/reports/jacoco/html")
}
}
}
//overall cumulative coverage tasks
jacocoTestCoverageVerification {
def execFilesToInclude = subprojects.collect {
it.name+"build/jacoco/*.exec"
}
executionData fileTree(project.rootDir.absolutePath).include(execFilesToInclude)
violationRules {
rule {
element = 'CLASS'
limit {
counter = 'LINE'
minimum = 0.8 //no matter which value I set, build does not fail
}
failOnViolation true
}
}
}
tasks.build.dependsOn jacocoMergeTestReport
tasks.build.dependsOn jacocoTestCoverageVerification