Why does gradle run every task in gradle.build

给你一囗甜甜゛ 提交于 2019-12-13 19:55:13

问题


I'm very new to gradle and have a basic question.

When I add a custom task to my gradle.build file and call "gradlw build" or "gradle clean" or any other gradle command, it automatically runs my custom task.

Is that how things work in gradle? run every task in the build file? Is there way to run a task only when I want it manually?


回答1:


task foo {
  println 'hello'
}

That creates a task, and during the configuration of the task, it tells gradle to execute println 'hello'. Every task is configured at each build, because gradle needs to know what its configuration is to know if the task must be executed or not.

task foo << {
  println 'hello'
}

That creates a task, and during the execution of the task, it tells gradle to execute println 'hello'. So the code will only be executed if you explicitly chose to run the task foo, or a task that depends on foo.

It's equivalent to

task foo {
  doLast {
    println 'hello'
  }
}

You chose not to post your code, probably assuming that gradle was acting bizarrely, and that your code had nothing to do with the problem. So this is just a guess, but you probably used the first incorrect code rather than the second, correct one.



来源:https://stackoverflow.com/questions/36674792/why-does-gradle-run-every-task-in-gradle-build

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