Skip to content

Instantly share code, notes, and snippets.

@ok3141
Forked from lifuzu/build.gradle
Created August 3, 2018 12:05
Show Gist options
  • Save ok3141/8918e107f4aae36a9f2ab7d234af7c81 to your computer and use it in GitHub Desktop.
Save ok3141/8918e107f4aae36a9f2ab7d234af7c81 to your computer and use it in GitHub Desktop.
Parse XML file in gradle
//Declaring the inputs and outputs of a task
//build.gradle
task transform {
ext.srcFile = file('mountains.xml')
ext.destDir = new File(buildDir, 'generated')
inputs.file srcFile
outputs.dir destDir
doLast {
println "Transforming source file."
destDir.mkdirs()
def mountains = new XmlParser().parse(srcFile)
mountains.mountain.each { mountain ->
def name = mountain.name[0].text()
def height = mountain.height[0].text()
def destFile = new File(destDir, "${name}.txt")
destFile.text = "$name -> ${height}\n"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment