Groovy: Quick and Easy Scripting

Recently I was tasked with some grungy file manipulation and text processing requiring some scripts. Ordinarily it would be the kind of thing I’d turn to Perl or a shell script for. However, it’s been a while since I have done anything substantial with either of those scripting languages/paradigms, so I’d need to refresh my memory.  I decided to try something new instead: Groovy. If I have to invest time into (re)learning something, it might as well be something new that I’m interested in (sorry, Perl and shell fans).

Recently I was tasked with some grungy file manipulation and text processing requiring some scripts. Ordinarily it would be the kind of thing I’d turn to Perl or a shell script for. However, it’s been a while since I have done anything substantial with either of those scripting languages/paradigms, so I’d need to refresh my memory.  I decided to try something new instead: Groovy. If I have to invest time into (re)learning something, it might as well be something new that I’m interested in (sorry, Perl and shell fans).

For those unfamiliar with Groovy, it is a scripting language built on top of the Java platform. Many are touting languages like Groovy as the future of the Java platform, a way to take advantage of the Java Virtual Machine and existing Java code and libraries without suffering the pain of actually writing more Java code, giving up on Java libraries, and/or porting applications to something else. The language syntax is not much of a stretch for a Java or Ruby developer; kind of a Java-like Ruby. For more details, go to groovy.codehaus.org/.  Here are some of the highlights as I see them, strictly from a quick-and-dirty scripting perspective:

  • Easy to install.  You download the distribution, set an environment variables, add the Groovy bin directory to your path, and go.  Obviously Java is a prerequisite, but that is also really easy to install and get going. Make sure the environment variable JAVA_HOME is defined.
  • Nice language syntax: compared to Perl and shell scripts, there is no comparison here.  Groovy is much easier to adapt to for those who are used to programming in proper programming languages. (Once again, my apologies to Perl lovers.)
  • Built-in regular expression support: Perl’s not the only game in town, you know.  Check out some of the things you can do: here and here.
  • Enhanced versions of basic Java classes like String, List, File, etc. for easier use.
  • Direct execution from the command line: Make sure groovy is in your path, make your script executable, and make this the first line in your script: #!/usr/bin/env groovy.  That’s it; no classpath, no wrapper scripts, nothing.

Here’s an example of a script that compares the contents of 2 files line-by-line and writes the lines not contained in both to a third file:

#!/usr/bin/env groovy<br>docs = read("docs.txt")<br>println "docs: " + docs.size()<br><br>converted = read("docsConverted.txt")<br>println "converted: " + converted.size()<br><br>unconverted = docs – converted<br>println "need to convert " + unconverted.size()<br><br>File output = new File("docsToConvert.txt")<br>output.withWriter { writer -><br>   unconverted.each { writer << it << 'n' }<br>}<br><br>println "Wrote " + output.readLines().size() + " lines"<br><br>def read(String filename) {<br>   new File(filename).readLines()<br>}

A couple quick notes about the above code snippet:

  • “it” is the default variable used inside a closure if you don’t specify one via the -> operator.
  • You don’t need to declare any classes, objects, or even a main() method/function.
  • Basic classes are available without imports, e.g., String, File
  • Data types are not required per se, but sometimes you need to specify them for various reasons, e.g., some method or operator has been overloaded. This is a little weird, but I think it’s better for the interpreter to complain about such things rather than oddly misbehave at runtime.

There are many other uses for Groovy, I am just scratching the surface here. Quickie scripting is an easy way to try it out with minimal commitment. I intend to explore integrating it with our Java code and trying out Grails when I get the chance.

Jed Prentice