Category Archives: Groovy


After not requiring Groovy for a while, I went about installing it today and was directed to GVM. Very interesting and quick to install. But essentially a RVM knockoff for java.

Replacing a text within a file in place

Recently I had to replace some text within a file, I also wanted this to occur in place. I ended up with two solutions. Mainly because of the Solaris version. But also because I wanted to play with Groovy again.

1. Perl on the commandline

perl -pi -e 's/1.2-SNAPSHOT/1.2/g' pom.xml

2. Using Groovy
Refer to Groovy: Writing to a File

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)

Thanks to David Heffelfinger