Thursday, March 29, 2012

Run Groovy from Ant's script task?

There is a minor lack of documentation regarding the Ant script task and running scripts using Groovy. On the Ant manual page for the "script" task, it says you can run something like this snippet:
<project>

  <target name="run">
   <script language="groovy">
    println "Hello world!"
   </script>
  </target>

</project>

It won't work. You'll get "Unable to create javax script engine for groovy".
So then you read further about library dependencies, but no real example of how to get it to work. This is the lack I'm writing about. No examples.

You have to include the groovy library jars or the single embeddable, groovy-all-x.x.x.jar.

So, to run the above script you would do something like:

ant -lib \java\GroovyVersions\Groovy-1.8.6\embeddable\groovy-all-1.8.6.jar run

Buildfile: C:\temp\build.xml

run:

[script] Hello world!

BUILD SUCCESSFUL

Total time: 0 seconds

Or modify the classpath, or use Ant's lib extension capability. Of course, why would you use the 'script' task when the 'groovy' task is available? You wouldn't.

Further Reading

Ant script task

Groovy Ant task

No comments: