This is just about the most FAQ from developers new to Gradle.
I’m not an expert, but I’ll give you a rough idea of what’s happening here.
Gradle executes the build script in two “phases”, being the “configuration” phase and the “execution” phase.
In the “configuration” phase, it executes all “raw” statements outside of task definitions and statements inside of task definitions, but outside of “doLast” (and “doFirst”, I suppose).
In the “execution” phase, when it’s determined the entire task dependency tree, it executes the “doLast” (and “doFirst”) blocks of tasks in dependency order.
In your test case, all of the unexpected output is defined outside of “doLast”/“doFirst”, so it is executed unconditionally in the “configuration” phase.