0

These three forms to define a task in build.gradle seem identical. All of them call org.gradle.api.internal.project.DefaultProject#task(java.lang.String, groovy.lang.Closure), but really I can't understand how the second and the third one can work.

def myAction = {t -> println "${t.name} [${t.class.name}]"}

task('myTaskA') {task ->
    group = 'MyTasks'
    description = name
    doLast myAction
}

task myTaskB {task ->
    group = 'MyTasks'
    description = name
    doLast myAction
}

task myTaskC() {task ->
    group = 'MyTasks'
    description = name
    doLast myAction
}
ninopg
  • 1
  • 2
  • 1
    https://stackoverflow.com/a/27584555/6509 – tim_yates Aug 14 '22 at 14:08
  • It is also worth mentioning that all these forms are discouraged. The recommended way to define a custom task is `tasks.register("myTask") { ... }`, which also doesn't look as magical as the older forms. – Bjørn Vester Aug 15 '22 at 08:09
  • Does this answer your question? [Understanding the groovy syntax in a gradle task definition](https://stackoverflow.com/questions/27584463/understanding-the-groovy-syntax-in-a-gradle-task-definition) – Jeff Scott Brown Aug 15 '22 at 12:50
  • @BjørnVester - where (link/resource) did you get the information from that tasks.register("..."){...} is recommended and the other task definition forms are discouraged? – becke-ch Nov 28 '22 at 21:20
  • @BjørnVester - I've myself answered my question see below. – becke-ch Nov 29 '22 at 06:42

1 Answers1

0

Task Configuration Avoidance: As of Gradle 5.1, it is recommend that the configuration avoidance APIs are used whenever tasks are created by custom plugins. The Configuration Avoidance API will co-exists with the existing APIs that will be replaced with the usual deprecation process over several major releases. In a nutshell, the API allows builds to avoid the cost of creating and configuring tasks during Gradle’s configuration phase when those tasks will never be executed, which can have a significant impact on total configuration time.

Instead of: “task ... {…}”, “task ... << {…}”, “task('...') {…}”, etc. use “tasks.register('...') {…}” API.

becke-ch
  • 413
  • 4
  • 8