k3b
k3b

Reputation: 14775

Nested dynamic properties in gradle scripts?

Is it possible to use nested properties in gradle build scripts to group global variables?

if gradle.properties file contains

mavenUpload.serverUrl=https://someServer.org/mavenrepository

can i use this in gradle scripts like this ${mavenUpload.serverUrl} or like this?

// in master gradle.build
task uploadArchives {
    description = "Upload to maven repository '${mavenUpload.serverUrl}'."

    // call all uploadArchives in the sub projects
    dependsOn { subprojects.uploadArchives }
}

Is it even possible to dynamically create nested properties like this

ext {
    mavenUpload {
        serverUrl='https://someServer.org/mavenrepository'
        login='myUsername'
        password='myTopSecretPassword'
    }
}

Is there a way to do something similar to this in gradle?

My current workaround is to use an underscore "_" instead of a point "."

What i have tried in gradle 2.13

ext {
    // works as expected
    myglobal='my global value'

    // error :-(
    myGlobalNested1 {
        myNested='my nested value'
    }

    // error :-(
    myGlobalNested2.ext {
        myNested='my nested value'
    }

    // error :-(
    myGlobalNested4.ext.myNested='my nested value'

    // error :-(
    myGlobalNested5.myNested='my nested value'

    myGlobalNested3 = new HashMap<String,Object>()      

}

// error :-(
myGlobalNested3.ext.myNested='my nested value'

task demo {
    // works as expected
    ext.mylocal='my local value'

    print """
        myglobal = ${myglobal}
        mylocal = ${mylocal}
        demo.mylocal = ${demo.mylocal}

        myGlobalNested.myNested = ${myGlobalNested.myNested}
    """
}

[Update 2016-06-11 @opal]

This script does not compile. The parts with // error :-( cause a compile error

Upvotes: 1

Views: 1802

Answers (2)

Hossein Shahdoost
Hossein Shahdoost

Reputation: 1742

I wouldn't recommend using ext inside another one. That would result some very weird behaviors from gradle. The top answer to this question gives us the right way of creating nested ext variables.

This would be the right way of making nested variables in gradle

ext {
    myarray = [
            name0     : "xx",
            name1     : "xx"
   ]
}

Upvotes: 3

Soda
Soda

Reputation: 21

I was also struggling with the same problem...

How is this?

ext {
    parent = ext {
        child = ext {
            grandChild = ext {
                greatGrandChild = "Kourtney"
            }
            grandChildBrother = "Khloé"
        }
        childSister = "Kim"
    }
}

and then

task demo {
    print """
            child sister is ${this.ext.parent.childSister}
            grand child brother is ${this.ext.parent.child.grandChildBrother}
            great grand child is 
            ${this.ext.parent.child.grandChild.greatGrandChild}
    """
}

Good luck!

Upvotes: 2

Related Questions