The Maven Cookbook
2.2. Running an Inline Groovy Script in a Maven Build
While Maven covers a lot of ground, there are certainly times when you just want to shell out to a script to get something simple done without having to write a custom plugin. Groovy is ideal for these situations because it provides you with a simple, dynamic scripting language that can be added directly to a project's POM.
Put some Groovy in your project's POM like this:
Example 2.1. Running a Groovy Script from a POM
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.mcookbook</groupId> <artifactId>groovy-script</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>groovy-script</name> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-model</artifactId> <version>2.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.groovy.maven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <id>groovy-magic</id> <phase>prepare-package</phase> <goals> <goal>execute</goal> </goals> <configuration> <source> def depFile = new File(project.build.outputDirectory, 'deps.txt') project.dependencies.each() { depFile.write("${it.groupId}:${it.artifactId}:${it.version}") } ant.copy(todir: project.build.outputDirectory ) { fileset(dir: project.build.sourceDirectory) } </source> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Example 2.1, “Running a Groovy Script from a POM” configures the GMaven Plugin's execute goal to execute during the prepare-package phase. The source configuration supplies a Groovy script which is run during the execution of the execute goal. Also note that the execution has an id element with a value of groovy-magic. This id element isn't required if you are only configuring one execution for a plugin, but it is necessary once you define more than one execution for a given plugin.
The Groovy script included, creates a file named "deps.txt" in
${basedir}/target/classes
, it then iterates through the project's
declared dependencies, and then it copies all of the source in
${basedir}/src/main/java
into the ${basedir}/target/classes
directory.
This script demonstrates the use of Groovy's closure syntax and ease
with which you can manipulate the filesystem. Groovy is an ideal
scripting language for manipulating text and working with files. To test
this script example, run mvn package as shown
below.
~/examples/groovy/groovy-script $ mvn package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building groovy-script
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform
dependent!
[INFO] skip non existing resourceDirectory ~/examples/groovy/groovy-script/src/main/resources
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform
dependent!
[INFO] skip non existing resourceDirectory ~/examples/groovy/groovy-script/src/test/resources
[INFO] [compiler:testCompile]
[INFO] Not compiling test sources
[INFO] [surefire:test]
[INFO] Tests are skipped.
[INFO] [groovy:execute {execution: default}]
[INFO] [jar:jar]
[INFO] Building jar: ~/examples/groovy/groovy-script/target/groovy-script-1.0-SNAPSHOT.jar
After running this example, list the contents of
${basedir}/target/classes/deps.txt
, and you should see the following
contents:
~/examples/groovy/groovy-script $ more deps.txt
org.apache.maven:maven-model:2.2.0