...
| Table of Contents | ||||
|---|---|---|---|---|
|
Default Variables
By default a few variables are bound into the scripts environment:
project | The maven project, with auto-resolving properties |
|---|---|
pom | Alias for project |
session | The executing |
settings | The executing |
log | A SLF4J Logger instance |
ant | An AntBuilder instance for easy access to Ant tasks |
fail() | A helper to throw MojoExecutionException |
Examples
These examples all using Maven executions which means that it binds the execute goal to a build phase. The configuration of source inside of an execution is only available for that execution. If you want to be able to mvn groovy:execute from the command-line, then you need something like:
| Code Block |
|---|
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<configuration>
<source>
println "Hi"
</source>
</configuration>
</plugin>
|
Execute an Inline Groovy Script
| Code Block |
|---|
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
if (project.packaging != 'pom') {
log.info('Copying some stuff...')
def dir = new File(project.basedir, 'target/classes/META-INF')
ant.mkdir(dir: dir)
ant.copy(todir: dir) {
fileset(dir: project.basedir) {
include(name: 'LICENSE.txt')
include(name: 'NOTICE.txt')
}
}
}
</source>
</configuration>
</execution>
</executions>
</plugin>
|
Execute a Local Groovy Script
| Code Block |
|---|
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${pom.basedir}/src/main/script/myscript.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
|
Execute a Remote Groovy Script
| Code Block |
|---|
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>http://mydomain.com/myscript.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
|
Class Dependencies
Using Java Classes
Scripts can use Java classes. To include custom classes to be available to the script define a classpath which contains additional artifacts to included in the scripts classloader.
...
| Code Block |
|---|
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<classpath>
<element>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</element>
</classpath>
<source>
import org.apache.commons.lang.SystemUtils
if (SystemUtils.IS_OS_WINDOWS) {
println('Go buy a Mac!')
}
</source>
</configuration>
</execution>
</executions>
</plugin>
|
Using Groovy Classes
By setting the scriptpath you can load additional Groovy classes.
...
| Code Block |
|---|
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scriptpath>
<element>${pom.basedir}/src/main/script</element>
</scriptpath>
<source>
import Helper
Helper.callSomeStatic()
</source>
</configuration>
</execution>
</executions>
</plugin>
|
Custom Properties
By setting the properties parameter additional project properties can be added for an execution.
...
| No Format |
|---|
mvn -Dhello=jason |
Script Helpers
Using fail()
Sometimes executed Groovy scripts need to fail the build. To do so with out causing evil stack traces or assert experssions to get spat out, you need to throw a MojoExecutionException. To help make this a little bit easier there is a fail() closure bound the the default namespace for the executed script.
...
| No Format |
|---|
try {
....
}
catch (Exception e) {
fail("She's the village bicycle! Everybody's had a ride.", e)
}
|
