Change subproject behavior if build ran from root

Hi folks, I’m new to Gradle but I’ve got a multi-project build almost complete. One of the things I’m looking for is the ability for my child projects to run an extra task if the build is executed from the child directory directly. If the project is ran from the root directory, this extra task should be suppressed. Is this approach possible or should I introduce a flag from the command line? I’m hoping to keep the build arguments as clean as possible and would like to avoid flags if possible.

Thanks in advance!

I don’t think there’s anything in Gradle that’ll help with this specifically. There are ways of listening to the build lifecycle but I’m afraid that it’ll be hard to use those to determine where it was executed.

The only thing I can think of, and it’s not very pretty, would be to use the “user.dir” system property.

In your child project’s build.gradle file you could do something like this:

if(["user.dir"].endsWith("child")) {
 build << {
  println "I built from the child project only"

This will add a doLast operation on the build command, only when gradle is executed from within the child project’s directory.

Note that this won’t work if you execute the child build via “:child:build” from within the root directory.

Also, I doubt it’ll work with the Tooling API, and so won’t work right from within any IDE’s using it.

Ah thanks, I stumbled across a similar solution using user.dir. Agree its not the cleanest, but will definitely do in the absence of a Gradle DSL approach.