How do I figure out why a task is being included i...
# community-support
r
How do I figure out why a task is being included in the configuration phase? I tried running
./gradlew tasks --dry-run
, but that just outputs
:tasks SKIPPED
.
v
What do you mean by "included"? Do you mean why it is configured? Or why it is registered at all? Or why it is executed? Or ...?
r
OK, more specifically - I have a db-migrate task. It (among others) is unintentionally being picked up in the configuration phase (https://docs.gradle.org/current/userguide/build_lifecycle.html). This means that when IntelliJ imports the project, db-migrate (and others) run. How do I figure out why gradle wants it to be in this particular phase, rather than being a build task, or even just a task that must be explicitly called? I can provide the build file, but I'm also interested in learning how to diagnose this...
v
Oh, you mean it is executed at configuration time?
That's hard to diagnose, because that is impossible. 😄
If this is an ad-hoc task, you probably just forgot to use
doLast { ... }
or
doFirst { ... }
for the task actions that should be done at execution time and thus do the work already when the task is configured.
r
Not following - what's impossible? I.e. there's no way to print out a task graph with diagnostics?
And, here's my migrate task, as one of the examples:
Copy code
tasks.register("db-migrate", DatabaseSetup::class) {
    println("Now migrating")
    migrate()
    jooqGenerate()
    println("Generated beautiful source")
    // don't run by default
    enabled = false
}
This should be picked up by every configuration phase run?
t
Yup, your actions (println / migrate / jooqGenerate) are executed at the time you configure the task: that's how you wrote it. Move those into a
doFirst
or
doLast
(depending on what
DatabaseSetup
already does) See e.g. https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:defining_tasks
r
ugh, so it runs part of each task in each phase? that's counter-intuitive. what about enabled=false - is that a no-op?
v
what's impossible?
That a task is executed at configuration time
what about enabled=false - is that a no-op?
No, it makes the task skip the execution phase. But as I and Thomas already said, you are not doing your stuff at execution time, but at configuration time, so it is executed each time you configure that task.
r
Well we have different definitions of "impossible", since this is being executed at configuration time (because, presumably, of what Thomas said).
doLast
doesn't sound like "anything not in this block is going to be run in configuration"
v
Well we have different definitions of "impossible", since this is being executed at configuration time
No, the task is not executed at configuration time as that is impossible. The configuration of that task is executed at configuration time. And the code you wonder executes is configuration code for the task, as you do not do it in execution-time action which you would register using
doLast
or
doFirst
as Thomas and I said above already. ;-)
Maybe you should have another look at the docs about implementing custom tasks. 🙂
r
I understand what he said, and already moved it to doLast, which caused
./gradlew tasks --all
to actually do what it was supposed to instead of outputting
:tasks SKIPPED
. As I said above: a) it's counter intuitive that code inside the task definition runs at configuration time. b) it's counter intuitive that
doLast
is for stuff that shouldn't run at configuration time. and to add to that c) it really sucks that simple things like this require looking at your extremely verbose documentation.
v
It is not my documentation, I'm just a user like you. If you find it unintuitive (I disagree) feel free to report it as issue. 😉 The usual way is, that you register a task and configure its properties, which is what you do in that block. One of the configurations you can do is registering additional actions to run at execution time, which in most cases you should not need as the task class usually should define the work done at execution time. If you expect the code in there to run at execution time, how would you imagine something like
enabled = false
could work? Setting it at execution time, especially as last line, is way too late, because you can hardly disable the task when it is already running. 😉
👍 1