gradlemulti-projectbuild-script

Gradle multi-project build: how to specify different build files?


Gradle multi-project build: how to set different build files? e.g.

/foo/bar/build-hello.gradle
/foo/bar/build-world.gradle

settings.gradle

include 'hello'
project(':hello').projectDir = new File('/foo/bar')
project(':hello').buildFile = new File('/foo/bar/build-hello.gradle')

include 'world'
project(':world').projectDir = new File('/foo/bar')
project(':world').buildFile = new File('/foo/bar/build-world.gradle')

Error: can not set readOnly property buildFile.

How to specify a different build file other than the default build.gradle?

EDIT:

https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:projectDir

File projectDir (read-only)
The directory containing the project build file.

projectDir is also readOnly. But no error in setting its value. Why?


Solution

  • The settings.gradle file operates on a Settings object, whose project() method returns ProjectDescriptor instances, because the Project instances are not created at this point of the Gradle build.

    The read-only buildFile property of the ProjectDescriptor instances is created from the two writable properties projectDir and buildFileName:

    include 'hello'
    project(':hello').projectDir = new File('/foo/bar')
    project(':hello').buildFileName = 'build-hello.gradle'
    
    include 'world'
    project(':world').projectDir = new File('/foo/bar')
    project(':world').buildFileName = 'build-world.gradle'