I have been very frustrated by Gradle and variable scoping in a multi-module project. I have wasted countless hours trying to follow examples that simply do not work as stated, and having limited examples for the type of multi-module project that I wanted to create.
So first I started with a List variable at the outermost part of my build.gradle called versions:
def versions = [root: "1.0.0"] | |
buildscript { | |
List list1 = ["com.baselogic:common:${versions.root}"] | |
List list2 = ["com.baselogic:extended:${versions.root}"] | |
println "--- buildscript ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
allprojects { | |
println "--- allprojects ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
subprojects { | |
println "--- subprojects ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
project('ch01') {} |
The issue is that ‘def versions’ was not accessible in buildscript, allprojects, or subprojects scope.
Then I moved this variable to the ‘buildscript’ section:
buildscript { | |
def versions = [root: "1.0.0"] | |
List list1 = ["com.baselogic:common:${versions.root}"] | |
List list2 = ["com.baselogic:extended:${versions.root}"] | |
println "--- buildscript ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
allprojects { | |
println "--- allprojects ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
subprojects { | |
println "--- subprojects ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
project('ch01') {} |
Now buildscript has access to that variable, but allprojects and subprojects both cannot access this variable.
So by removing ‘def’ from versions like to have: versions = [root: “1.0.0”] versions was found by all scopes but now the next variable issue.
‘List list1’ and ‘List list2’ were both accessible to the buildscript but not to allprojects or subprojects.
UGGG!!!!!
So by removing the ‘List’ identifyer, finally all scopes had access to the variables I have set. Here is what I finally ended up with:
buildscript { | |
versions = [root: "1.0.0"] | |
list1 = ["com.baselogic:common:${versions.root}"] | |
list2 = ["com.baselogic:extended:${versions.root}"] | |
println "--- buildscript ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
allprojects { | |
println "--- allprojects ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
subprojects { | |
println "--- subprojects ---" | |
println "versions: $versions" | |
println "list1: $list1" | |
println "list2: $list2" | |
} | |
project('ch01') {} |
Which gave me this output:
C:\>gradle -t -a | |
--- buildscript --- | |
versions: [root:1.0.0] | |
list1: [com.baselogic:common:1.0.0] | |
list2: [com.baselogic:extended:1.0.0] | |
--- allprojects --- | |
versions: [root:1.0.0] | |
list1: [com.baselogic:common:1.0.0] | |
list2: [com.baselogic:extended:1.0.0] | |
--- allprojects --- | |
versions: [root:1.0.0] | |
list1: [com.baselogic:common:1.0.0] | |
list2: [com.baselogic:extended:1.0.0] | |
--- subprojects --- | |
versions: [root:1.0.0] | |
list1: [com.baselogic:common:1.0.0] | |
list2: [com.baselogic:extended:1.0.0] | |
:tasks | |
------------------------------------------------------------ | |
All tasks runnable from root project | |
------------------------------------------------------------ |
I hope this can save someone else some frustration with variable scoping in a multi-module project.
Recent Comments