Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Variable Scoping With a Multi-Module Project

DZone's Guide to

Variable Scoping With a Multi-Module Project

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

 

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}