Integrating Groovy in your Maven builds using GMaven

July 12th, 2011 by

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.



You only need a few things to run the following examples..

Project Setup

Of course we’re going to need a mavenized project first ..

  • Create a new simple Maven project using your Maven-enabled-IDE or via console and
    mvn archetype:generate
  • I have added a name element in the pom.xml because I want to use its value in the first example .. so my pom.xml looks like this
    <project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    <name> Groovy Maven Plugin Samples</name>

Running Groovy Scripts

Now that we’ve got a mavenized project let’s take a look at the different possibilities to load Groovy scripts into our Maven build process..

Useful Variables

There are a few helpful variables that we’re able to use in our Groovy scripts this list is taken from the more detailed GMaven documentation

  • ant: Accessor for the AntBuilder to access ant tasks
  • fail(): Allows you to throw MojoExecutionExceptions
  • log: slf4j logger instance
  • pom: An alias for project
  • project: Your maven project with auto-resolving properties
  • session: The executing maven session
  • settings: The executing settings

Inline Scripts

In our first example we want to define a simple task inside if our pom.xml. The executed Groovy code is going to print the project’s name, version and the current build time into the console..

  • Simply add the following markup to your pom.xml
                                 println "I am building version ${project.version} of ${} at ${new Date()}"
  • Now run
    mvn package
  • You should see the following output (of course with a different date)
    [INFO] --- gmaven-plugin:1.3:execute (run-groovy) @ groovy-maven-tutorial ---
    I am building version 0.0.1-SNAPSHOT of Groovy Maven Plugin Samples at Tue Jul 12 21:15:29 CEST 2011

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 ${} 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
  • Now we’re running mvn package again and we should see the following output
    I am building version 0.0.1-SNAPSHOT of Groovy Maven Plugin Samples at Tue Jul 12 21: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.


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

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.


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.

Article Updates

  • 2015-04-01: Code formatting fixed, Link to Gradle added.

    Tags: , , , , , , ,