Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Executing Groovy code from Maven is a snap with GMaven.

Groovy source can be configured inline from POM configuration, or from an external file or URL source, have additional property configuration, better defaults and well just makes you feel happy (smile)

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 MavenSession

settings

The executing Settings

log

A SLF4J Logger instance

ant

An AntBuilder instance for easy access to Ant tasks

fail()

A helper to throw MojoExecutionException

Examples

Execute an Inline Groovy Script

Execute a Local Groovy Script

Execute a Remote Groovy Script

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.

This is the preferred mechanism to extend a scripts classpath instead of adding dependencies to the plugin, which may cause problems if the classpath needs to be different on more than one plugin execution or when it differs between usage in more than one module in a multi-module build.

Artifact definitions will pick up missing details from the POM's dependencyManagement section.

Using Groovy Classes

By setting the scriptpath you can load additional Groovy classes.

For example if you have a class named Helper defined in a file named ${pom.basedir}/src/main/script/Helper.groovy your scripts can use that class:

Custom Properties

By setting the properties parameter additional project properties can be added for an execution.

Properties will automatically resolve when accessed. This means that if you have defined a property with a value of ${session} }} that when you access it from your script, the returned value will be the {{MavenSession instance, just as one would expect when using @parameter expression="${session}" in a Mojo annotation. Though as noted below, default values are handled slightly differently than what is done currently for Mojo annotations.

WARNING

Icon

Property expressions are not GStrings, they are Maven expressions.
You need to use the ${foo} syntax for them, $foo will not work.

Because of this property resolution, the value of a property might not be a String as one might expect when talking about normal Java properties. But, just for us Groovy doesn't really care what the type is.

By setting defaults additional project properties can be added for an execution which will be used only if the project or system does not override.

To provide a system override:

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.

Failing with a simple string:

fail("That ain't no woman! It's a man, man!")

Failing with an exception detail:

try {
    ....
}
catch (Exception e) {
    fail(e)
}

Failing with an exception detail and a message:

try {
    ....
}
catch (Exception e) {
    fail("She's the village bicycle! Everybody's had a ride.", e)
}

Resources

Goal Documentation

Groovy Development

  • No labels