First Time Linux

Groovy

Groovy is a very new scripting language based on the java platform. Basically it allows you to write simple scripts quickly, which can then be run as if it had been written in java. For a lot of simple tasks this speeds up development and allows greater flexibility.

For example, in order to just print something out, you don't have to write a class file with a main() method which calls System.out.println(), you just have a single line in a script with the "println" command.

Getting and running

Groovy isn't available from the official repositories, so you'll have to go the old route of downloading from a website. First you need a java runtime (from the repositories), and then you can get the groovy binaries from the download site at groovy.codehaus.org.

At the time of writing, the current stable version is 1.5.7. All you need to do is download and unzip the provided zip file and you can get started.

First we can check the version number by calling the groovy executable:

$ ~/download/groovy-1.5.7/bin/groovy -v
Groovy Version: 1.5.7 JVM: 1.6.0_06

You could of course set up an alias or symlink so you don't need the full path to groovy, but this demonstrates that it works. The next thing to try is the interactive console:

$ ~/download/groovy-1.5.7/bin/groovyConsole
groovy console

This brings up a simple java application which acts like a scratchpad for trying things out, as shown in the screengrab to the right.

Just type in your code, and press Ctrl-R to run it. The output will appear in the lower pane of the application.

In this example we define an array and loop over the elements printing out each one. When Ctrl-R is pressed, the commands are repeated in the lower pane together with the generated output.

Running a saved script

Now let's save our commands in a file, so we can run it any time we like. Use an editor (doesn't matter whether it's a graphical one like Kate or a console one like vi, use whatever you prefer) and create a new file called veggies.groovy:

stringList = [ "carrots", "peas", "turnips", "corn", "cabbage",
   "courgettes", "kohlrabi", "romanesco", "beans", "broccoli",
   "cauliflower", "parsnips" ]
stringList.sort().findAll{it.size() > 4}.each() { print it + ", " }
println ""

After saving this file, we can give it straight to the interpreter:

$ groovy veggies.groovy
beans, broccoli, cabbage, carrots, cauliflower, courgettes, kohlrabi, parsnips, romanesco, turnips,

This simple example shows the definition of a simple string array, the looping (with the "each" keyword, the sorting and the filtering based on a length criteria. This is a much shorter bit of code than would be required by a real java program.

GUI applications

Because groovy runs on the java platform, it can integrate with other java platform components, including standard java classes. So the GUI stuff is already available in the standard awt and swing libraries. These are accessible from groovy with simple import statements, and they work just as they do in java. There are also some helper classes provided by groovy including SwingBuilder to make the gui code simpler.

More info

The official home page is groovy.codehaus.org which has a lot of useful information including of course documentation. There's also Wikipedia's Groovy page for some basic background.