Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed exception message in fail()
Excerpt

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)

Table of Contents
maxLevel2
minLevel1

Default Variables

By default a few variables are bound into the scripts environment:

...

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.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <configuration>
        <source>
            println "Hi"
        </source>
    </configuration>
</plugin>

Execute an Inline Groovy Script

Code Block

<plugin>
    <groupId>org.codehaus.gmaven</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.gmaven</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.gmaven</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>

...

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

Code Block

<plugin>
    <groupId>org.codehaus.gmaven</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>

...

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:

Code Block

<plugin>
    <groupId>org.codehaus.gmaven</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>

...

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.

Code Block

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <properties>
                    <hello>world</hello>
                </properties>
                <source>
                    println project.properties['hello']

                    // or if you prefer, these work just the same:
                    // println project.properties.hello
                    // println project.properties.get('hello')
                    // println project.properties.getProperty('hello')
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

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.

Code Block

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <defaults>
                    <hello>world</hello>
                </defaults>
                <source>
                    println project.properties['hello']
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

To provide a system override:

No Format

mvn -Dhello=jason

Script Helpers

...

Failing with a simple string:

No Format

fail("ThatSome ain'tmeaningful nomessage woman! It's a man, man!about the fatal error.")

Failing with an exception detail:

No Format

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

Failing with an exception detail and a message:

No Format

try {
    ....
}
catch (Exception e) {
    fail("She'sSome themeaningful villagemessage bicycle! Everybody's had a rideabout the fatal error.", e)
}

Resources

Goal Documentation

...