How gradle handles multiple build.gradle files in multi build project


(p-sharma) #1

I have this project structure

-->Starnderd Location
    -->Project1 
       -->settings.gradle 
       -->build.gradle
       -->Subproject11
          -->build.gradle
       -->Subproject12
          -->build.gradle
    -->Project2 
       -->settings.gradle 
       -->build.gradle
       -->Subproject21
          -->build.gradle
       -->Subproject22
          -->build.gradle
    -->build.gradle
    -->settings.gradle

Following the directions mentioned here http://stackoverflow.com/questions/12154031/multiple-settings-gradle-files-for-multiple-projects-building/29664571#29664571 i was able to get the correct settings.gradle.

However, what happens to build.script file in Project1, Project 2 & Project 3. Do they participate in the build if gradle command is run at root project level.

If i do this in my root Project build.gradle file , i get plugins not found error for Sub-project Subproject11

apply from : Project1/build.gradle
apply from : Project2/build.gradle

Does it mean, buildscript block needs to be copied from Subproject11 to the root project buildscript and apply from will not work at root project build.gradle file


(Dimitar Dimitrov) #2

What are you trying to achieve with this snippet? In practice, you are applying the settings defined in these two files to the root project (which doesn’t make sense imho).

Multi-project builds are defined in settings.gradle - each include statement there creates a ProjectDescriptor that you can manipulate further and tweak to your liking (have a look at the Settings object API and implementation for details).

After the settings are evaluated, the ProjectDescriptors are converted to a corresponding graph of Project objects and most (all?) of the properties that you could tweak in settings.gradle are frozen.


(p-sharma) #3

My understanding of “apply from” was incorrect. I was under impression that it will include the content of another build.gradle file. So thanks for correcting me there.

To give some background information, i have 3 separate multi projects, they all have their own settings.gradle, project level build.gradle and module level build.gradle.
Now the objective is to merge these three multi projects into once common code base and make one build script to build all of them together (the desired project structure is mentioned in my post earlier). So my thought was to include the contents of other build scripts by using apply.

Based on what i have learned on gradle so far it looks like there is no use of build.gradle file at Project1 level because it cannot be included and setting defined in this file should be copied to root level build.gradle file. Essentially have a build.gradle at root level and another one at module level (Subproject11).


(Dimitar Dimitrov) #4

Have a look at a few bigger projects (for example Gradle, Groovy and Spring) to see how multi-project builds are typically implemented.

Your use-case looks much like the one for “composite projects”, which is still available only through the Buildship plugin.

You can fake it by creating a settings file in an empty directory and including all the projects you need. If your projects are using each other, you would need to create a build which uses project substitution to tie them together. If all this sounds like mumbo-jumbo, you would need to read a book (or at least google all these terms in the reference manual and look through the samples bundled with the Gradle distribution).

Good luck!