{"id":9893,"date":"2020-02-02T07:47:46","date_gmt":"2020-02-02T07:47:46","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=9893"},"modified":"2021-11-13T05:48:03","modified_gmt":"2021-11-13T05:48:03","slug":"what-is-gradle-dag-directed-acyclic-graph","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-gradle-dag-directed-acyclic-graph\/","title":{"rendered":"What is Gradle DAG (Directed Acyclic Graph)?"},"content":{"rendered":"\n<p><strong>What is DAG (Directed Acyclic Graph)<\/strong><br> 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.<\/p>\n\n\n\n<p><strong>Understanding DAG (Directed Acyclic Graph) in Gradle<\/strong><\/p>\n\n\n\n<p>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). <\/p>\n\n\n\n<p>What this means is that a build essentially configures a set of tasks and wires them together \u2014 based on their dependencies \u2014 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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"450\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/gradle-dag-diagram.jpg\" alt=\"\" class=\"wp-image-9895\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/gradle-dag-diagram.jpg 779w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/gradle-dag-diagram-300x173.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/gradle-dag-diagram-768x444.jpg 768w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p>In order to understand &#8220;Directed Acyclic Graphs&#8221; DAG, we need to understand a Gradle build lifecycle.<\/p>\n\n\n\n<p>Gradle build lifecycle consist of three phases: initialization, configuration, and execution:<\/p>\n\n\n\n<p><strong>The initialization phase with settings.gradle<\/strong><\/p>\n\n\n\n<p>During Initialization, Gradle decides which projects are to participate in the build.<\/p>\n\n\n\n<p>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\u2019s 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.<\/p>\n\n\n\n<p><strong>The configuration phase with build.gradle<\/strong><\/p>\n\n\n\n<p>During Configuration, task objects are assembled into an internal object model, usually called the DAG (for directed acyclic graph).<\/p>\n\n\n\n<p>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 \u201cGradle executes the build scripts\u201d 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 \u201cdirected\u201d term means each dependency arrow goes in one direction. \u201cAcyclic\u201d means that there are no loops in the graph.<\/p>\n\n\n\n<p><strong>The execution phase with doLast and doFirst<\/strong><\/p>\n\n\n\n<p>During Execution, build tasks are executed in the order required by their dependency relationships.<\/p>\n\n\n\n<p>In nutshell, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If you put a code in the settings.gradle file, it is evaluated in the initialization phase<\/li><li>The code in your build script files that are not related to actions of your tasks are evaluated in the configuration phase.<\/li><li>The code in the actual actions of your tasks like the doLast closures of your tasks are evaluated in the execution phase.<\/li><\/ul>\n\n\n\n<p><strong>Single project build Example of Gradle DAG (Directed Acyclic Graph)<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/7707b0e88292a5f1d2a76e15d1d5900a.js\"><\/script>\n\n\n\n<p><strong>Output of gradle test<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">&gt; gradle test\n\n<span class=\"hljs-attr\">E<\/span>:\\gra&gt;gradle test\nThis is executed during the initialization phase.\n\n&gt; Configure project :\nThis is executed during the configuration phase.\nThis is also executed during the configuration phase.\nThis is executed during the execution phase.\n\n&gt; Task :test\nThis is executed during the execution phase <span class=\"hljs-keyword\">with<\/span> doFirst\nThis is executed during the execution phase <span class=\"hljs-keyword\">with<\/span> doLast\n\nBUILD SUCCESSFUL <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span>s\n<span class=\"hljs-number\">1<\/span> actionable task: <span class=\"hljs-number\">1<\/span> executed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>To list task dependencies in Gradle?<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ gradle dependencies - Displays all dependencies declared in root project <span class=\"hljs-string\">'gra'<\/span>.\n$ gradle dependencyInsight - Displays the insight into a specific dependency in root project <span class=\"hljs-string\">'gra'<\/span>.\n$ gradle dependentComponents - Displays the dependent components of components in root project <span class=\"hljs-string\">'gra'<\/span>. &#91;incubating]\n\nOnly lists dependencies in master project  \n$ gradle dependencies\n$ gradle dependencies --scan\n\nLists subproject dependencies in project\n$  gradle :&lt;subproject&gt;:dependencies\n$ .\/gradlew app:dependencies\t<span class=\"hljs-comment\"># where app is your project module.<\/span>\n\n<span class=\"hljs-keyword\">If<\/span> you want to see dependencies on project <span class=\"hljs-keyword\">and<\/span> all subprojects <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">in<\/span> <span class=\"hljs-title\">your<\/span> <span class=\"hljs-title\">top<\/span>-<span class=\"hljs-title\">level<\/span> <span class=\"hljs-title\">build<\/span>.<span class=\"hljs-title\">gradle<\/span>:\n<span class=\"hljs-title\">subprojects<\/span> {\n    <span class=\"hljs-title\">task<\/span> <span class=\"hljs-title\">listAllDependencies<\/span>(<span class=\"hljs-title\">type<\/span>: <span class=\"hljs-title\">DependencyReportTask<\/span>) {}\n}\n\n<span class=\"hljs-title\">Then<\/span> <span class=\"hljs-title\">call<\/span> <span class=\"hljs-title\">gradle<\/span>:\n$ <span class=\"hljs-title\">gradle<\/span> <span class=\"hljs-title\">listAllDependencies<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_84027\"><iframe loading=\"lazy\"  id=\"_ytid_16359\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_84027\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":9931,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5460],"tags":[],"class_list":["post-9893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gradle"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/9893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=9893"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/9893\/revisions"}],"predecessor-version":[{"id":24977,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/9893\/revisions\/24977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/9931"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=9893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=9893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=9893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}