Help understanding Multi-project build

Hi there,

I’ve been reading up on tutorials from Gradle site. While the example in there seems to work fine, but I’m struggling to apply it in real world scenario. I will try to give as details as possible and I may have numerous questions. But I’d appreciate if you can help me understand what I’m doing wrong here.

First of all, here’s my directory structure

/TestSchedule – build.gradle – gradle.properties – settings.gradle – MergingProgram/ ---- build.gradle – ScheduleReader/ ---- build.gradle ---- gradle.properties

As you can see, I have 2 sub-directories under “TestSchedule”. I’m treating those as “sub-projects” but they’re not dependent on each other in order to perform their own tasks. I also want to use properties files separately that are relevant particular subprojects only. In my root project’s settings.gradle, here’s what I used.

include 'MergingProgram', 'ScheduleReader'

If I run the command “gradle” from root project, it evaluates both of these projects including the root project. However, if I run “gradle” from ScheduleReader directory, it evaluates the sibling project named MergingProgram as well. What is that the case? Isn’t it supposed to evaluate “down the hierarchy”?

I then altered settings.gradle in my root project as shown below.

includeFlat 'MergingProgram', 'ScheduleReader'

Now, if I run gradle from one of the subprojects, they don’t seem to evaluate other projects. Only the project directory I’m working from gets evaluated, which is what I was expecting initially.

Happily working on each of those subprojects’ build.gradle file to add necessary tasks and methods. I tested the tasks by running gradle from those subprojects directory and made sure they gave me the output I’m expecting.

I wanted to use root project’s build.gradle file to act like the executioner. The intent is to execute various tasks from sub-projects in certain order to complete the build activity. I am having problem at this stage and when I ran “gradle --info”, it seems the root project thinks sub-projects are using an “empty” build file, which obviously is not true. Is this because of “includeFlat” in the settings file?

So, why can I not run any tasks of the sub-projects from the root project? Am I approaching this the wrong way or did I misunderstood the concept? How am I supposed to handle this? If you have any questions from me for more clarification, please let me know. Thank you,

Good question.

Gradle searches upwards for a settings.gradle. So in your first case, it found the settings.gradle and evaluated both MergingProgram and ScheduleReader. Gradle, by default, evaluates all projects. There are incubating features to only evaluate projects on demand.

When you changed the settings.gradle to includeFlat and changed directory into a subproject, Gradle still searched upwards and found the settings.gradle, but it couldn’t find any of the subprojects listed. Not sure why we silently ignore this, but this basically means your root project was completely ignored (TestSchedule) and your subproject was the only project evaluated.

Back in the root project, includeFlat means that the included projects should be at the same level (siblings to TestSchedule). Since it can’t find the projects build.gradle, it assumes they don’t exist (and are empty).

Since you want to hook everything together through the root project, your best bet is to make sure the daemon is turned on. That’ll speed up the configuration time required for evaluating all of the subprojects between runs. You might be able to try configure-on-demand, but this requires discipline in your build scripts. You could also turn off the upwards search we do (-u) if your subprojects are kept completely separate and you don’t configure subprojects from the root project.

Thank you Sterling for the explanation. I’m going to need a bit more time to digest all of that info and definitely need some practice.

I’m a beginner at Graddle and haven’t worked with Goovey before. So, I haven’t been able to piece together a lot of things even after lots of googling (and the gradle guide as well). I’m going to be asking a lot of questions here :). Thanks for your patience.

There are a lot of samples in the Gradle distribution (samples/ in the Gradle ‘all’ zip). You can also see them here: https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples

Thanks. I’ll take a look