About Gradle's setup phase and execution phase

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.

Example

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

why

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

About Gradle's setup phase and execution phase
About Bean and DI
About classes and instances
About gets and gets.chomp
About redirect and forward
About encapsulation and inheritance
About Serializable and serialVersionUID
Ubuntu 18.04.05 About LTS setup
About for statement and if statement
Setup of JMeter and jEnv
About synchronized and Reentrant Lock
About Ruby hashes and symbols
[Java] About String and StringBuilder
About the same and equivalent
jMetal Explanation-Algorithm setting and execution
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
About Java Packages and imports
About Ruby and object model
About Ruby classes and instances
About instance variables and attr_ *
About self-introduction and common errors