Ways to invoke Groovy from your Build

March 26, 2008 at 6:55 pm | Posted in Build, Groovy, Java, Software Development | 1 Comment
Tags: , , , , ,

There might be several reasons for you wanting to invoke Groovy from your Java build. Either you’re fed up with coding XML or you want to take a shortcut around the build systems sometimes cumbersome way to extend basic functionality. Custom Ant tasks and Maven Plugins hardly can overcome the shortcomings of XML expressiveness. Groovy can truly enrich your build through scripting. Java/Groovy hybrid applications also need a way to compile Groovy classes. No problem…support exists.

1. Ant


The Groovy-All library comes with the Ant task groovyc that lets you compile your Groovy source files. It can be used very similar to the javac Ant task.

<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpathref="my.classpath"/>

<groovyc srcdir="${src.dir}" destdir="${bin.dir}">


The library also comes with the Ant task groovy that lets you execute your Groovy code. For more information see “Groovy in Action”, chapter 13.1.7 which gives a thorough code example. Previously compiled Groovy classes can be executed like regular Java classes using the Ant task java. First define the task definition in your build script:

<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="my.classpath"/>

Afterwards you can use all Groovy capabilities in your Ant targets:

   println "Your Groovy code here!"

2. Maven 2

A very convenient way to configure Maven 2 to compile and execute your Groovy classes or scripts is to use the Groovy Maven Plugin. You have to stick to the Maven conventions to place your Groovy (test) sources in the right folders. Put your Groovy sources in <project-root>/src/main/groovy and your test sources to <project-root>/src/test/groovy.

First you need to add the Groovy Maven plugin and the Groovy runtime to your POM dependencies. Please check the Maven repository for the last versions:



To compile your Groovy sources add the Groovy Maven plugin to your POM build definition. To start compilation run mvn compile in your shell.



There are several ways to execute your Groovy code:

a) Execute an inline Groovy script
b) Execute a local or remote Groovy script
c) Execute compiled Groovy classes

To execute your compiled Groovy classes you can simply use the Maven Exec plugin. Your Groovy classes are nothing else than regular Java class files. Run mvn exec:java in your shell.

3. Gant

Gant is a build tool that sits on top of Ant. You can use all tasks Ant provides. Instead of using XML to code your targets you use Groovy as scripting language. Therefore, the execution of Groovy syntax works right away. You don’t need to add additional notation to make this work.


For compiling our Groovy classes to Java code we again use the groovyc.

sourceDirectory = 'src/main/groovy'
buildDirectory = 'bin'
Ant.taskdef(name : 'groovyc', classname : 'org.codehaus.groovy.ant.Groovyc')

target(compile : 'Compile source to build directory.') {
   groovyc(srcdir : sourceDirectory , destdir : buildDirectory)


I think it’s needless to say how to invoke Groovy in Gant. Simply use it!


1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. Unadulterated words, some truthful words man. You rocked my day!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: