Skip a task when running another task

北城以北 提交于 2020-07-15 11:17:04

问题


I added a task to my gradle project:

task deploy() {
    dependsOn "build"
    // excludeTask "test"  <-- something like this

    doFirst {
       // ...
    }
}

Now the build task always runs before the deploy task. This is fine because the build task has many steps included. Now I want to explicitly disable one of these included tasks.

Usually I disable it from command line with

gradle deploy -x test

How can I exclude the test task programmatically?


回答1:


You need to configure tasks graph rather than configure the deploy task itself. Here's the piece of code you need:

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(deploy)) {
        test.enabled = false
    }
}



回答2:


I don't know what your deploy task does, but it probably just shouldn't depend on the 'build' task. The 'build' task is a very coarse grained lifecycle task that includes tons of stuff you probably don't want.

Instead it should correctly define its inputs (probably the artifacts that you wanna deploy) and then Gradle will only run the necessary tasks to build those inputs. Then you no longer need any excludes.



来源:https://stackoverflow.com/questions/40649712/skip-a-task-when-running-another-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!