Make composite build references optional

I’m currently playing with the composite build feature.

In my “main-project” build I have a settings.gradle file which looks like this:

rootProject.buildFileName = 'buildscripts/build.gradle'
includeBuild '../utils-lib'

This is working fine if utils-lib is an existing folder with its own gradle build. However if the folder is missing I get the following exception:

$ ./gradlew build

FAILURE: Build failed with an exception.

* Where:
Settings file 'D:\workspace\main-project\settings.gradle' line: 5

* What went wrong:
A problem occurred evaluating settings 'main-project'.
> Included build 'D:\workspace\utils-lib' does not exist.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 12.17 secs

This reduces the usability a lot in complex projects where I would want to be able to only checkout the main project and run with it until I actually need to change on of its many dependencies. Then I would want to checkout only this specific dependency and work on main-project and my-current-dependency using composite builds for rapid development.

I could change the settings.gradle file when I checkout the dependency. But due to the

rootProject.buildFileName = 'scripts/build.gradle'

directive I want the settings.gradle file to be part of my VCS repo. Editing the file would obviously mark it as changed in my VCS. But I can’t commit this change since my co-workers won’t necessarily be working on the same dependency. Thus the file will remain changed forever. My co-workers would have the same problem on their end and we would need to be careful to never ever commit changes to this file.

Did I miss something or is my problem currently not solvable with composite builds?

Just add a check that the directory exists

if (new File(rootDir, '../utils-lib').exists()) {
   includeBuild '../utils-lib'
} 
2 Likes

That works. I’m stupid. Somehow it didn’t occur to me I could put code into the settings.gradle file. Despite its name my brain handled it like a settings.properties file. Thanks a lot for your help.