Chapter 13
The Build Lifecycle

We said earlier, that the core of Gradle is a language for dependency based programming. In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that those tasks are executed in the order of their dependencies and are executed only once. Those tasks form an Directed Acyclic Graph. There are build tools that build up such a DAG as they execute there tasks. Gradle builds the complete DAG before any task is executed. This lies at the heart of Gradle and makes many things possible which would not be possible otherwise.

Your build scripts configure this DAG. Therefore they are strictly speaking build configuration scripts.

13.1 Build Phases

A Gradle build has three distinct phases.

Initialization
Gradle supports single and multi-project builds. During the initialization phase, Gradle determines which project(s) are going to take part in the build. Also during this phase, Gradle creates Project objects for every project taking part in the build.
Configuration
The build scripts of all projects which are part of the build are executed. This configures the project objects.
Execution
A subset of the tasks, created and configured during the configuration phase, is executed. The subset is determined by the task name arguments passed to the gradle command and the current directory.

13.2 Settings File

Beside the build script files, Gradle defines a settings file. The settings file is determined by Gradle via a naming convention. The default name for this file is settings.gradle. Later in this chapter we explain, how Gradle looks for a settings file.

The settings file gets executed during the initialization phase. A multiproject build must have a settings.gradle file in the root project of the multiproject hierarchy. It is required because in the settings.gradle file it is defined, which projects are taking part in the multi-project build (see chapter 14). For a single-project build, a settings.gradle file is optional. You might need it for example, to add libraries to your build script classpath (see chapter 15). Let’s first do some introspection with a single project build:

  println 'This is executed during the initialization phase.'

  println 'This is executed during the configuration phase.'
  
  createTask('test') {
      println 'This is executed during the execution phase.'
  }

  >gradle test
  Modern compiler found.
  Recursive: true
  Buildfilename: gradlefile
  This is executed during the initialization phase.
  No build sources found.
  :: loading settings :: url = jar:file:/Users/hans/IdeaProjects/gradle-release/gradle-core-RB-0.1/build/
  :: resolving dependencies :: org.gradle#build;SNAPSHOT
  confs: [build]
  ++++ Starting build for primary task: test
  ++ Loading Project objects
  ++ Configuring Project objects
  This is executed during the configuration phase.
  Project=: evaluated.
  ++ Executing: test Recursive:true Startproject: :
  Executing: :test
  This is executed during the execution phase.
  BUILD SUCCESSFUL
  Total time: 1 seconds

For a build script, the property access and method calls are delegated to a project object. Similarly property access and method calls within the settings file is delegated to a settings object. Have a look at org.gradle.api.Settings.

13.3 Multi-Project Builds

A multi-project build is a build where you build more than one project during a single execution of Gradle. You have to declare the projects taking part in the multiproject build in the settings file. There is much more to say about multi-project builds in the chapter dedicated to this topic (see chapter 14).

13.4 Initialization

How does Gradle know whether to do a single or multiproject build? In particular as you may not just trigger a multiproject build from the root project, but also from within any subproject taking part in the build.1 . If you execute Gradle from within a project that has no settings.gradle file, Gradle does the following:

What is the purpose of this behavior? Somehow Gradle has to find out, whether the project you are into, is a subproject of a multiproject build or not. Of course, if it is a subproject, only the subproject and its dependent projects are build. But Gradle needs to create the build configuration for the whole multiproject build (see chapter 14). Via the -u command line option, you can tell Gradle not to look in the parent hierarchy for a settings.gradle file. The current project is then always build as a single project build. If the current project contains a settings.gradle file, the -u option has no meaning. Such a build is always executed as:

Gradle creates Project objects for every project taking part in the build. For a single project build this is only one project. For a multi-project build these are the projects specified in Settings object (plus the root project). Each project object has a name equals to the name of its top level folder. The projects form a tree according to there position in the file system. Thus every project except the root project has a parent project and might have child projects. There are no gaps in the project tree. This means every parent folder between a specified project folder and the project root is considered to be a project and is automatically part of the project tree. This means no additional work for you, as a project might have a build script but it is not mandatory (see chapter 14).

13.5 Configuration and Execution of a Single Project Build

For a single project build, the workflow of the after initialization phases are pretty simple. The build script is executed against the project object that was created during the initialization phase. Then Gradle looks for tasks with names equals to those passed as command line arguments. If these task names exist, they are executed as a separate build in the order you have passed them. The configuration and execution for multi-project builds is discussed in chapter 14.