Basic Usage

NOTE: This plugin includes the Ant runtime, so you can use the AntBuilder.

Default Variables

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

  • project - The maven project, with properties pre-resolved
  • pom - Alias for project
  • log - The maven plugin logger

Execute an Inline Groovy Script

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <source>
                    <body>
                        if (project.packaging != "pom") {
                            log.info("Copying some stuff...")

                            def ant = new AntBuilder()
                            
                            def dir = "${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")
                                }
                            }
                        }
                    </body>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

Execute a Local Groovy Script

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <source>
                    <file>${pom.basedir}/src/main/script/myscript.groovy</file>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

Execute a Remote Groovy Script

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <source>
                    <url>http://mydomain.com/myscript.groovy</url>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

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 prefered 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.

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <classpath>
                    <element>
                        <groupId>commons-lang</groupId>
                        <artifactId>commons-lang</groupId>
                    </element>
                </classpath>
                <source>
                    <body>
                        import org.apache.commons.lang.SystemUtils
                        if (SystemUtils.IS_OS_WINDOWS) {
                            println("Go buy a Mac!")
                        }
                    </body>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

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:

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <scriptpath>
                    <element>${pom.basedir}/src/main/script</element>
                </scriptpath>
                <source>
                    <body>
                        import Helper
                        Helper.callSomeStatic()
                    </body>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

Custom Properties

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

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <properties>
                    <hello>world</hello>
                </properties>
                <source>
                    <body>
                        println(project.properties['hello'])
                    </body>
                </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.

<plugin>
    <groupId>org.apache.geronimo.genesis.plugins</groupId>
    <artifactId>script-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>groovy</goal>
            </goals>
            <configuration>
                <defaults>
                    <hello>world</hello>
                </defaults>
                <source>
                    <body>
                        println project.properties['hello']
                    </body>
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

To provide a system override:

mvn -Dhello=jason