The reason for this behavior is that initDb
isn't declared correctly. It's missing a <<
, and hence the println
statement gets run at configuration time rather than execution time. This also means that the statement always gets run. This doesn't mean that the task gets executed (in the second example it doesn't).
To avoid such mistakes, I recommend to use the more explicit and regular doLast
syntax in favor of <<
:
task profile {
// code in here is about *configuring* the task;
// it *always* gets run (unless `--configuration-on-demand` is used)
dependsOn initdb
doLast { // adds a so-called "task action"
// code in here is about *executing* the task;
// it only gets run if and when Gradle decides to execute the task
println "profile"
}
}