Script references seem to always be relative to root project

I have a script that is included from a main script via apply from: ‘gradle/scripts/foo.gradle’. How can I have the foo.gradle script reference another script bar.gradle via apply from when bar.gradle is in the same folder as foo.gradle? It seems that all paths are always resolved against the root project.

build.gradle (references gradle/scripts/foo.gradle --> OK) gradle/scripts/foo.gradle (references bar.gradle --> only works if I write apply from: ‘/gradle/scripts/bar.gradle’) gradle/scripts/bar.gradle

Of course, if I reference the script via path relative to the root project it works, but that is not very pretty since the script needs to know where it is located relative to the root project.

This is by design as it makes things more consistent.

One thing you can do is create a primitive method to include these things that knows about your project layout.

allprojects {
  includeScript = { apply from: "$rootDir/gradle/scripts/${it}.gradle" }

If you do that first in your master build.gradle, you can then anywhere just do:

includeScript "foo"
includeScript "bar"

Ah, nice. I like it.