Build with Gradle is divided into three phases of initialization, setting, and execution, and builds are performed in that order.
Especially the setting phase and the execution phase are easily confused, If you are not aware of the distinction, the build.gradle settings you wrote may not work as intended.
If you write the following in build.gradle and execute gradle myTask
, how will it be output?
println name + ": " + "FIRST"
task myTask {
println name + ": " + "First"
doFirst {
println name + ": " + "doFirst block"
}
doLast {
println name + ": " + "doLast block"
}
println name + ": " + "Last"
}
println name + ": " + "LAST"
The result is as follows.
gradle-test2: FIRST
myTask: First
myTask: Last
gradle-test2: LAST
:myTask
myTask: doFirst block
myTask: doLast block
BUILD SUCCESSFUL
In the above example, doFirst and doLast were evaluated in the execution phase, and the others were evaluated in the configuration phase.
Another Task description method, myTask << {}
, is equivalent to doLast, but there is a warning that it will be abolished in the future in the environment of gradle 3.3.
Recommended Posts