Gradle 改变另一个行为的任务

Gradle task that changes behavior of another one

你能帮我配置两个构建失败或忽略错误的checkstyle任务吗?不幸的是,我没有足够的知识来自己做。

我有 checkstyle 配置

apply plugin: 'checkstyle'
checkstyle {
    toolVersion = "7.1.1"
    sourceSets = [sourceSets.main]
    ignoreFailures = true
    showViolations = true
    reportsDir = file("$project.buildDir/checkstyleReports")
    configFile = file("$rootDir/gradle/checkstyle.xml")
    configProperties = ['baseDir': "$project.projectDir"]
}

Checkstyle 插件添加了另一个任务:checkstyleMain、checkstyleTest。

我需要创建扩展 checkstyleMain 但覆盖 属性 ignoreFailures

的新任务

我看到了下一个方法(但是不行):

checkstyleMain {
    ignoreFailures = true
}

task forceCheckstyleMain(type: Checkstyle) {
    ignoreFailures = false
}

你可以在这里看到完整的讨论线程和其他想法https://discuss.gradle.org/t/gradle-task-that-changes-behavior-of-another-one/20295/7

回答我的问题可以通过以下方式解决

checkstyleMain {
    gradle.taskGraph.whenReady { graph ->
        ignoreFailures = !graph.hasTask(":" + project.name + ":forceCheckstyleMain")
    }
}

task forceCheckstyleMain(dependsOn: 'checkstyleMain') {
}

也许这不是最好的解决方案,但它很短而且有效