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.
Prerequisites
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.tutorial</groupId> <artifactId>groovy-maven-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hasCode.com Groovy Maven Plugin Samples</name> </project>
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
<build> <plugins> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <id>run-groovy</id> <goals> <goal>execute</goal> </goals> <phase>package</phase> <configuration> <source> println "I am building version ${project.version} of ${project.name} at ${new Date()}" </source> </configuration> </execution> </executions> </plugin> </plugins> </build>
-
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 hasCode.com 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 ${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 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.
<source>http://localhost/remote.groovy</source>
Tutorial Sources
I have put the source from this tutorial on my GitHub repository – download it there or check it out using Mercurial:
git clone https://github.com/hascode/groovy-maven-plugin.git
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.
Article Updates
-
2015-04-01: Code formatting fixed, Link to Gradle added.