Posted by: bgopi | November 7, 2014

Reading properties from xml file using gradle

Here is how to read properties defined in xml file and store them in ext scope so that you can access in your gradle build script later.

file: build.gradle

task parseXml() {
def parsedPropXml = (new XmlParser()).parse(‘properties.xml’)
parsedPropXml.property.each{ prop ->
project.ext.set(prop.@name, prop.@value)
}
println “<<print props>>”
project.ext.properties.each {k, v ->
println “$k: $v”
}
//retrieve individual property from ext
def propValue = project.ext.properties.get(“property_name”)
println “property_name: $propValue ”
}

execute:

prompt> gradle parseXml

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: