I\'m trying to craft a Gradle multiproject build for a situation in which my project layout is already dictated to me. I have something like this:
-->Shar
A couple of pointers:
projectDir
for all projects, not just the root project. You should use relative paths, e.g. rootProject.projectDir = new File(settingsDir, "../foo")
and project(":sub1").projectDir = new File(rootDir, "bar")
. Here, settingsDir
refers to the directory containing settings.gradle
, and rootDir
is a shorthand for rootProject.projectDir
.(root)Project.children
. Note that settings.gradle
and build.gradle
use different types to represent a project - ProjectDescriptor and Project, respectively.settings.gradle
, or a subdirectory thereof. From a usability perspective, it is therefore best to put settings.gradle
into the root of the directory hierarchy.For more information, see Settings in the Gradle Build Language Reference, and the Multi-Project Builds chapter in the Gradle User Guide.
For completeness, the settings.gradle
that solved my specific example above is as follows:
rootProject.name = 'Product1'
def projectTreeRootDir = new File( "${ProjectsRoot}" )
// Shared components
def sharedRootDir = new File( projectTreeRootDir, 'Shared' )
include ':SharedComponent1'
project( ':SharedComponent1' ).projectDir = new File( sharedRootDir, 'SharedComponent1' )
include ':SharedComponent2'
project( ':SharedComponent2' ).projectDir = new File( sharedRootDir, 'SharedComponent2' )
// Product components
includeFlat 'ProductComponent1', 'ProductComponent2'
Clearly this doesn't scale to large numbers of subprojects and it could be done significantly better using the hints provided by Peter above.