I add some dependencies (containing our plugins) to the buildscript classpath. I then want to refer to some class I’ve added in the script and set some ‘ext’ property. For example, lets say I just want to get a reference to ‘SomeEnum.values()’ and ‘SomeEnum’ is on my classpath.
As far as I can see, this does not work when the code is shifted to a gradle script that is applied and I can’t see why this would be so. For example, this works;
// build.gradle
ext {
someValues = com.mycomponent.SomeEnum.values()
}
someValues.each {
// do something with it
}
but this does not
// build.gradle
apply from: projects.gradle
// projects.gradle
ext {
someValues = com.mycomponent.SomeEnum.values()
}
someValues.each {
// do something with it
}
The latter blows with
* What went wrong:
A problem occurred evaluating script.
> cannot get property 'com' on extra properties extension as it does not exist
Officially, each build script needs to declare its build script dependencies on its own. In the current implementation, a child build script can also see the parent script’s dependencies, but this isn’t the case for applied build scripts.
It seems to be designed that way, the build script’s ClassLoader is used only for project’s build script and not for any other script, see AbstractProject.beforeCompile
This seems slightly counterintuitive and/or inconsistent though given that the applied script references things (plugins) that only exist by virtue of the classpath specified in the applying script.
It won’t work because it’s an applied build script, but the class path is only inherited by child build scripts (i.e. build scripts of child projects).