I have root project and subproject (:child
).
Root build looks like like this:
def foo = {
println("foo")
}
allprojects {
task bar << {
println(project.name + ":bar")
}
afterEvaluate {
foo()
}
}
Running gradle bar
prints:
foo
foo
:bar
:child:bar
child:bar
parent:bar
This make sense. However, IRL I need foo
to be called by the child's build file (because I want it to be called only by some of the submodules).
The documentation seems to be clear enough: In a multi-project build, sub-projects inherit the properties and methods of their parent project
However, moving the "afterEvaluate" block above into child/build.gradle
results in an error: Could not find method foo() for arguments [] on project ':child' of type org.gradle.api.Project
.
Why does this happen and how do I fix this? I have tried a whole bunch of different variations - moving the def
around (to buildscript
, allprojects
, to ext
, to allprojects.ext
, making it a variable in ext, instead of a method etc.), referring to it differently (as rootProject.foo
, rootProject.foo()
, ext.foo()
etc.) - nothing seems to work.
Any ideas?
Vars need to be declared in the ext namespace for them to be propagated downstream. Try:
ref: https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html