Question

I've got two simple tasks

task initdb { println 'database' }
task profile(dependsOn: initdb) << { println 'profile' }

During runtime the result in console looks like this

enter image description here

When my tasks looks like this

task initdb { println 'database' }
task profile() << { println 'profile' }

the result in console is

enter image description here

How to skip initdb task when it's not used in the task profile in runtime? (without using -x)

Was it helpful?

Solution

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"
    }    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top