Declare array/hashmap in gradle.properties file
The notation {myelement1=myvalue1, myelement2=myvalue2, myelement3=myvalue3}
is simply a string representation of the object as the result of calling Map.toString()
. It is not syntactically correct Groovy.
Your first example is the correct way to define a Map
.
def myMap = [ key : 'value' ]
Defining an array is similar.
def myArray = [ 'val1', 'val2', 'val3' ]
Set the property to JSON string
myHash = {"first": "Franklin", "last": "Yu"}myArray = [2, 3, 5]
and parse it in build script with JsonSlurper:
def slurper = new groovy.json.JsonSlurper()slurper.parseText(hash) // => a hashmapslurper.parseText(array) // => an array
The JsonSlurper way is good, but I wanted a cleaner way to define both a simple string or an array as a property. I solved it by declaring the property as:
mygroup=myvalue1
or:
mygroup=myvalue1,myvalue2,myvalue3
Then inside Gradle retrieve the property with:
Properties props = new Properties()props.load(new FileInputStream(file('myproject.properties')))props.getProperty('mygroup').split(",")
And you will get an array of String. Be careful with space characters around the commas.