The Maven Cookbook

2.4. Writing Plugins in Groovy

2.4. Writing Plugins in Groovy

Groovy is a dynamic language based on the Java Virtual Machine which compiles to Java bytecode. Groovy is a project in the Codehaus community. If you are fluent in Java, Groovy will seem like a natural choice for a scripting language. Groovy takes the features of Java, pares down the syntax a bit, and adds features like closures, duck-typing, and regular expressions. For more information about Groovy, please see the Groovy web site at

2.4.1. Creating a Groovy Plugin

To create a Maven Plugin using Groovy, you only need two files: a pom.xml and a single Mojo implemented in Groovy. To get started, create a project directory named firstgroovy-maven-plugin. Place the following pom.xml in this directory.

Example 2.5. POM for a Groovy Maven Plugin

<?xml version="1.0" encoding="UTF-8"?>
  <name>Example Groovy Mojo - firstgroovy-maven-plugin</name>

What's going on in this POM? First, notice that the packaging of the POM is maven-plugin because we are creating a project that will package a Maven plugin. Next, note that the project depends on the groovy-mojo-support artifact in the org.codehaus.mojo.groovy group.

Then under src/main/groovy in a directory org/sonatype/mavenbook/plugins, create a file named EchoMojo.groovy which contains the EchoMojo class.

Example 2.6. 

package org.sonatype.mavenbook.plugins

import org.codehaus.mojo.groovy.GroovyMojo

 * Example goal which echos a message
 * @goal echo
class EchoMojo extends GroovyMojo {

     * Message to print
     * @parameter expression="${echo.message}"
     *            default-value="Hello Maven World"
    String message

    void execute() { message )