Groovy: Writing to a File

Recently had a requirement at work to update multiple maven pom files with the correct <version>. After performing a grep for the files that I needed to change I wrote this simple grroovy script to open each file replace the text and then save the file.


def fileName = "pom_location.txt" // Locations of the pom files
def sourceText = "1.2-SNAPSHOT"
def replaceText = "1.2"

new File(fileName).eachLine { line ->

def pomFile = line
pomFile = pomFile.replaceAll("\\s","") + "/pom.xml"
def file = new File(pomFile)
println "Editing ... $pomFile"
def fileText = file.text
fileText = fileText.replaceAll(sourceText, replaceText)
file.write(fileText)
}

Thanks to David Heffelfinger http://ensode.net/roller/dheffelfinger/entry/groovy_script_to_find_and