Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle multi project - execute root task only once

I am working with the following project structure Project

---Subproject1

---Subproject2

---Subproject3

|build.gradle

|settings.gradle

Subproject2 depends on Subproject 1

Subproject3 depends on Subproject 2

For all subprojects I need to have task "build", which will be custom for each project. But some initialization task (same for all subprojects) must be executed before "build". Thing is - this initialization task must be executed once and only once during build, no mater what I'm building - root project or any of subprojects. For example, if I do build of Subproejct3, sequence must be following: init build Subproject1 build Subproject2 build Subproject3

I tried to define "doFirst" for "build" at root project build.gradle and put "init" code into it. But in this case "init" code executed before each "build". I tried to define dependency on root for each "build" but faced same problem as previously.

like image 886
mrzodiak Avatar asked Sep 29 '22 23:09

mrzodiak


1 Answers

This is quite simple with Gradle. Define your init task (probably in root project) and customize build tasks in your subprojects to depend on this init like build.dependsOn ':init' More details at http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html

BTW: every tasks can be executed only once when a build is started so it seems that you have added your task action using doFirst() multiple times or added it to multiple tasks that were executed.

like image 115
Radim Avatar answered Oct 07 '22 20:10

Radim