rakesh kumar

Posted on

# How Directed Acyclic Graph Plays role Gradle Lifecycle

Understanding DAG (Directed Acyclic Graph) in Gradle

settings.gradle is defined in Which phase of Lifecycle

where we decide and whih phase of lifeyle gradle builds single or multiple projets

In which phase task objects are assembled and in which life cycle

In which phase Build Gradle is created
what is A DAG and meaning of Acyclic

In which phase dependency relationships to perform actual actions of your tasks

In which phase doLast and doFirst are performed

what is doLast and doFirst in whih life cycle
*In which phase Jar File is created *

## What is DAG (Directed Acyclic Graph)

In computer science and mathematics, a directed acyclic graph (DAG) is a graph that is directed and without cycles connecting the other edges. This means that it is impossible to traverse the entire graph starting at one edge. The edges of the directed graph only go one way. The graph is a topological sorting, where each node is in a certain order.

## Understanding DAG (Directed Acyclic Graph) in Gradle

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 these tasks are executed in the order of their dependencies, and that each task is executed only once. These tasks form a Directed Acyclic Graph. Gradle builds the complete dependency graph before any task is executed. Gradle models its builds as Directed Acyclic Graphs (DAGs) of tasks (units of work).

What this means is that a build essentially configures a set of tasks and wires them together — based on their dependencies — to create that DAG. Once the task graph has been created, Gradle determines which tasks need to be run in which order and then proceeds to execute them.

In order to understand “Directed Acyclic Graphs” DAG, we need to understand a Gradle build lifecycle.

Gradle build lifecycle consist of three phases:

`````` initialization,
configuration
execution:
``````

During Initialization, Gradle decides which projects are to participate in the build.

In this phase, Gradle tries to identify all the projects involved in the build process. It is very important for Gradle to know whether it’s a Single-project build or a Multi-project build. In a Multi-project build there are several projects to evaluate. Hence, several build scripts. Gradle looks at the settings.gradle file in order to identify the different projects. At the end of the initialization phase, Gradle creates an instance of org.gradle.api.Project corresponding to each of these projects.

During Configuration, task objects are assembled into an internal object model, usually called the DAG (for directed acyclic graph).

During this phase, Gradle executes the build script of each project identified in the previous phase. Actually, it is very important to know that just because we say “Gradle executes the build scripts” does not mean that the Tasks in those build scripts are executed too. Instead, after evaluating those scripts as simple Groovy scripts and identify the tasks in it, Gradle builds a Directed Acyclic Graph (DAG) of task objects. A DAG is a mathematical algorithm for representing a graph that contains no cycles. The “directed” term means each dependency arrow goes in one direction. “Acyclic” means that there are no loops in the graph.

The execution phase with doLast and doFirst

During Execution, build tasks are executed in the order required by their dependency relationships.

In nutshell,

If you put a code in the settings.gradle file, it is evaluated in the initialization phase
The code in your build script files that are not related to actions of your tasks are evaluated in the configuration phase.

Single project build Example of Gradle DAG (Directed Acyclic Graph)

``````println 'This is executed during the initialization phase.'
``````

``````println 'This is executed during the configuration phase.'

println 'This is also executed during the configuration phase.'
}
``````
``````task test {
println 'This is executed during the execution phase.'
doLast {
println "This is executed during the execution phase with doLast "
}
doFirst {
println "This is executed during the execution phase with doFirst "
}
}
``````

``````> gradle test

This is executed during the initialization phase.

> Configure project :
This is executed during the configuration phase.
This is also executed during the configuration phase.
This is executed during the execution phase.

This is executed during the execution phase with doFirst
This is executed during the execution phase with doLast

BUILD SUCCESSFUL in 1s

\$ gradle dependencies - Displays all dependencies declared in root project 'gra'.
\$ gradle dependencyInsight - Displays the insight into a specific dependency in root project 'gra'.
\$ gradle dependentComponents - Displays the dependent components of components in root project 'gra'. [incubating]

Only lists dependencies in master project

Lists subproject dependencies in project

If you want to see dependencies on project and all subprojects use in your top-level build.gradle:
subprojects {
}

``````

settings.gradle is defined in Which phase of Lifecycle
The initialization phase

where we decide and whih phase of lifeyle gradle builds single or multiple projets
The initialization phase

The initialization phase

In which phase task objects are assembled and in which life cycle

The configuration phase

In which phase Build Gradle is created

The configuration phase

what is A DAG and meaning of Acyclic

A DAG is a mathematical algorithm for representing a graph that contains no cycles

“Acyclic” means that there are no loops in the graph.

In which phase dependency relationships to perform actual actions of your tasks
The execution phase

In which phase doLast and doFirst are performed

The execution phase

what is doLast and doFirst in whih life cycle

The execution phase

In which phase Jar File is created

The execution phase