Maven scripting in Groovy

Recently, on Maven's user list, someone wanted to be able to script Maven with Groovy, like it was already possible with BeanShell or Jython thanks to specific Jelly tags. But unfortunately, Maven didn't provide that kind of support for Groovy.

But there are good news on that front since Jeremy Rayner contributed a nice Groovy Ant task.

That Ant task will be available in the upcoming Groovy release (jsr-02), so if you want to play with Maven and Groovy, you'll have to build Groovy from sources in the meantime. But I'm going to reveal how to script Maven with Groovy in advance.

In your maven.xml, you can access Maven's POM:

<?xml version="1.0" encoding="UTF-8"?>
<project default="groovy" xmlns:ant="jelly:ant">
 <goal name="groovy">
   <ant:taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="maven.dependency.classpath"/>
       println pom.eachPropertyName{ println it }
Don't forget to add Groovy in your dependencies:

Currently, only the POM is added in the script context, but other variables or properties may be added, but you can already access almost everything from Maven's project. Don't hesitate to ping me if you've got some specific requests.


© 2012 Guillaume Laforge | The views and opinions expressed here are mine and don't reflect the ones from my employer.