找不到方法 jacocoTestCoverageVerification()

Could not find method jacocoTestCoverageVerification()

我想让 jacoco 插件在测试覆盖率不够时失败。我使用 gradle 页面中的示例: https://docs.gradle.org/current/userguide/jacoco_plugin.html

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("output/jacoco/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.enabled true
        html.destination file("output/jacoco/jacocoHtml")
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.5
            }
        }

        rule {
            enabled = false
            element = 'CLASS'
            includes = ['org.gradle.*']

            limit {
                counter = 'LINE'
                value = 'TOTALCOUNT'
                maximum = 0.3
            }
        }
    }
}

但是,我得到错误:

Could not find method jacocoTestCoverageVerification() for arguments [build_4v41fim1xdl76q49oxk7mnylv$_run_closure6@18601994] on root project 'demo' of type org.gradle.api.Project.

谁能指点一下?

根据 the docs for Gradle 版本 3.4 或更高版本 :

For projects that also apply the Java Plugin, The JaCoCo plugin automatically adds the following tasks:

jacocoTestReport [...]

jacocoTestCoverageVerification [...]

如果您问题中的代码块显示了完整的 build.gradle 文件,这意味着您需要添加应用 Java 插件 (apply plugin: 'java') 的行。

当然,你也可以自己添加一个名为jacocoTestCoverageVerificationJacocoCoverageVerification类型的任务到你自己的构建文件中:

task jacocoTestCoverageVerification(type: JacocoCoverageVerification) {
    // configuration
}

尝试使用最新版本的 gradle。这可能会解决您的问题