Over a million developers have joined DZone.

Variable Scoping With a Multi-Module Project

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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.

 

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Mick Knutson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}