Composite builds defined externally?

Hello,

I am trying composite builds for the first time. I understood the need to put includeBuild into settings.gradle of the project including my library:

def folder_name = '../library'
if(new File(folder_name).exists()) {
    build.dependsOn gradle.includedBuild('library').task(':build')
}

Neat feature, but I am trying to give members of my team full freedom on using this feature at all, and if so, be free to put their library project whenever they want on their local machine.

Is it possible to configure the composite build outside of the project, like in the local user gradle.properties file?

Sort of.

You can specify any path you like from the command-line, like --include-build ../some/path. That’s fine for one off/temporary situations.

You can specify a path that you look at, by convention, e.g., you’ll include all builds that are checked out into $HOME/checkedOutProjects. This could potentially be something that’s configurable per-person.

Lastly, you could also externalize the list of included builds into a file that you check for, e.g. $HOME/my-included-builds.txt could contain the fully qualified paths to all included builds. In that case, you could read the file in settings.gradle and call includedBuild for each.

As an aside, I think your example might be referring to the build.gradle? Either way, you should avoid the use of new File(...) because it will always be relative to the current working directory of the daemon which may not be your project directory. You should use file() if you need a File-like thing instead. includeBuild already does this in settings.gradle, so you can just pass the file path (includedBuild '../library').

You also don’t want to litter your build.gradle with checks for included builds. If you want to have a build task that depends on all of the other included builds build tasks, you can just do build.dependsOn gradle.includedBuilds*.task(":build"). If there aren’t any included builds, you won’t depend on anything (this returns an empty list).

HTH

2 Likes

Hello, thank you for your answer!

The doc also suggests that includeBuild checks the existence of the build to include, but when trying includeBuild('../library') only without checking the existence of the folder myself I get the following error:

Error:Included build ‘/Users/myuser/gradle/library’ does not exist.

This check is quite important because we don’t want to have to use composite builds on the CI Server, and that is the whole point of the feature, right?

Neat trick, I’ll use this from now on!

Alex

Yes, sorry for being unclear, I meant more in your build.gradle files. It would be a little strange if you had checks to file paths everywhere.

I am a little surprised in settings.gradle includeBuild does not check itself if the folder exists, that we all have to wrap it in a file() check. Is there a plan to add this check in includeBuild and silently fail on error?

Not at this time, it’s more useful to check that the included build exists/is valid so users aren’t surprised if they fat-finger the path.

1 Like