Integrating Groovy in your Maven builds using GMaven

Often ant tasks are used in Maven builds but wouldn’t it be more attractive to integrate the Groovy language into our build process?

GMaven is the answers to this problem and brings together Maven and Groovy. It allows us to execute Groovy scripts inline from our Maven configuration, from a local script or even from a remote location. In the following short examples I am going to show how to configure Maven to execute Groovy scripts from different locations.

Local Scripts

Now we’re going to execute a local Groovy script from a location in the project directory ..

First we’re ading a new directory named script in src/main and in this directory a new file named example.groovy

println"I am building version ${project.version} of ${project.name} at ${new Date()} using example.groovy"

To create a reference to the groovy file we just need to modify the source tag of our first example

<source>${pom.basedir}/src/main/script/example.groovy</source>

Now we’re running mvn package again and we should see the following output

I am building version 0.0.1-SNAPSHOT of hasCode.com Groovy Maven Plugin Samples at Tue Jul 1221:41:06 CEST 2011 using example.groovy

Remote Scripts

Loading scripts from a remote location is quite easy .. you just have to change the source tag again and add the URL to your script .. e.g.

<source>http://localhost/remote.groovy</source>

Tutorial Sources

I have put the source from this tutorial on my Bitbucket repository – download it there or check it out using Mercurial:

hg clone https://bitbucket.org/hascode/groovy-maven-plugin

Alternative: Gradle

If you don’t have a strong reason for your project to use Maven you definitely should consider using Gradle as your build tool. Written in Groovy it allows you to write up your build scripts within a few lines of Groovy code and there is a variety of different plugins available.

Resources

Very detailed and helpful information can be found at the GMaven project website – if you want to take a deeper look at possible configurations and other features I highly recommend to stop by there.