1

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?

Yoaz Menda
  • 1,555
  • 2
  • 21
  • 43

1 Answers1

5
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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    You are right. I wasn't aware of the difference between those two task definitions. Thanks for your explanation. – Yoaz Menda Apr 17 '16 at 09:34