Monday, March 10, 2008

Groovy Ant task is for Groovy Scripts

Groovy includes an Ant task for executing either file based or embedded Groovy. I recently discovered the file option denoted by the src attribute must be a Groovy script and not a Groovy class containing a main method. If you have used the Java task, you might expect the Ant arg set to be passed as a parameters to the main method since it is the common Ant convention for passing command line arguments. However, as you can see in the following snippet of code from org.codehaus.groovy.ant.Groovy, a GroovyShell is created and the args are passed as a property to the script.
final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
try {
final Script script = groovy.parse(txt, scriptName);
script.setProperty("ant", new AntBuilder(this));
// code removed for brevity
script.setProperty("args", cmdline.getCommandline());
script.run();
}
This behavior was unexpected since it does not follow the normal Ant convention and because the documentation states the src attribute is a File containing Groovy statements rather than stating it expects a Groovy Script.

No comments: