Sunday, June 1, 2008

How to use groovy in a JGrass plugin

Those that tried Groovy will probably agree with me to the fact that for certain things groovy is makes things really really fast against standard java.

I was wondering if it is possible to mix groovy code inside a JGrass plugin and use it without problems as if it was java. Since groovy can be compiled into java bytecode, I thought it would work and be worth the try.

Here the results:

1) I first created a plugin called eu.hydrologis.jgrass.groovy inside which I put the following groovy libraries:
  • asm-2.2.jar
  • groovy-1.5.5.jar
2) installed the groovy plugin for eclipse from

3) created my test plugin eu.hydrologis.jgrass.groovy_test

4) rightclick on the plugin project and under teh entry Groovy -> Add Groovy Nature

5) added the eu.hydrologis.jgrass.groovy to my dependencies

6) I didn't really want to test something spatial, just if the classloader would work properly.
So I simply created a udig operation.

public class GroovyOp implements IOp {

public void op(final Display display, Object target,
IProgressMonitor monitor) throws Exception {

final GroovyTest gT = new GroovyTest();

display.asyncExec(new Runnable() {

public void run() {
MessageBox msg = new MessageBox(display.getActiveShell(),
String s = "Sorted cars in XML:\n";
for (String str : gT.getSort()) {
s = s + str + "\n";


Where the class GroovyTest is in fact a groovy class: GroovyTest.groovy

package eu.hydrologis.jgrass.groovy_test

import eu.hydrologis.jgrass.groovy_test.GroovyTest
class GroovyTest{
private String[] stuff = null;

public GroovyTest()
def cars = '''

Production Pickup Truck with speed of 271kph

Isle of Man
Smallest Street-Legal Car at 99cm wide and 59 kg in weight

Most Valuable Car at $15 million


def records = new XmlSlurper().parseText(cars)

def sortedNames ={ it.@year.toInteger() }.'@name'*.text()
println it

stuff = sortedNames as String[]

public String[] getSort() {
return stuff;

public static void main(String[] args){
new GroovyTest();
Since I am doing just an xml test and my code parser for coloring goes mad with xml, I attach a screenshot of the groovy code:

7) I then run the test operation on any layer (I called the operation groovy test, find it in the list), not so important, this is all dummy testing:

8) and see it there, the result is as expected, one line to parse an xml file... now that is nice!


Update: from the groovy eclipse plugin site:

Create a Groovy Project

To create a basic Groovy project in Eclipse perform the following steps:

  • Go to: File -> New -> Project
  • Select Java Project and press Next
  • In the Project Name field enter the name of your project (GroovyJava for this example)
  • Under Project Layout select Create separate source and output folders and press Finish
  • In the Package Explorer find the newly created project, right click, and select Groovy -> Add Groovy Nature

So far you should have a src folder, a bin-groovy folder, and several libraries. There is also a bin folder that was created by Eclipse, but is hidden due to exclusion filters. The next steps are needed to make the bin-groovy folder the default output folder, and to connect it to the src folder so that the debugger will know where to find the associated source and classes:

  • In the Package Explorer, right click on the "GroovyJava" project, and select: Build Path -> Configure Build Path
  • Select the Source tab and then use the Browse button to change the Default Output Folder from bin to bin-groovy
  • Press OK, OK

This will expose the bin folder in the Package Explorer. I'm not sure why the plugin creates a bin-groovy directory. Perhaps there are other "bin" files that are best kept separate from the Groovy classes, or perhaps one of the original versions of Eclipse didn't create a "bin" directory automatically. Some day when someone has a clear idea of the usefulness of this, or lack thereof, we can clean up my instructions.

No comments: