Debug School

rakesh kumar
rakesh kumar

Posted on

How Directed Acyclic Graph Plays role Gradle Lifecycle

what-is-gradle-dag-directed-acyclic-graph
understanding-gradle-the-build-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

what is org.gradle.ap

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.

Image description

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:
Enter fullscreen mode Exit fullscreen mode

The initialization phase with settings.gradle

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.

The configuration phase with build.gradle

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.
The code in the actual actions of your tasks like the doLast closures of your tasks are evaluated in the execution phase.

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

settings.gradle

println 'This is executed during the initialization phase.'
Enter fullscreen mode Exit fullscreen mode

build.gradle

println 'This is executed during the configuration phase.'

task configured {
    println 'This is also executed during the configuration phase.'
}
Enter fullscreen mode Exit fullscreen mode
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 "
    }
}
Enter fullscreen mode Exit fullscreen mode

view rawgradle-dag-example.gradle hosted with ❤ by GitHub

Output of gradle test

> gradle test

E:\gra>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.

> Task :test
This is executed during the execution phase with doFirst
This is executed during the execution phase with doLast

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
To list task dependencies in Gradle?

$ 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  
$ gradle dependencies
$ gradle dependencies --scan

Lists subproject dependencies in project
$  gradle :<subproject>:dependencies
$ ./gradlew app:dependencies    # where app is your project module.

If you want to see dependencies on project and all subprojects use in your top-level build.gradle:
subprojects {
    task listAllDependencies(type: DependencyReportTask) {}
}

Then call gradle:
$ gradle listAllDependencies 


Enter fullscreen mode Exit fullscreen mode

Image description

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

what is org.gradle.ap

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

Top comments (0)