Fork me on GitHub

Execute

Groovy script execution is the main purpose of GMaven and is available with the execute goal. A script can be executed as part of a build (attached to a lifecycle phase) or directly via the command-line.

This goal is not bound to a lifecycle phase by default, so the phase must always be configured, and the phase will depend highly on what the purpose is that you are executing a script for.

This goal works with and without a project. When a project is available additional classpath configuration options are avaiable.

All context variables are availble for use in executed scripts.

Source

The configuration of the source of the script to execute can be configured to be one of:

  • File
  • URL
  • Inline

This value can be specified by the source configuration parameter or by the source property.

File

Files can be used, and for most complex usage recommend, as the source of the script to execute:

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>generate-resources</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>${project.basedir}/src/main/script/myscript.groovy</source>
        </configuration>
      </execution>
  </executions>
</plugin>

or:

mvn groovy:execute -Dsource=src/main/script/myscript.groovy

This form only works when the file actually exists and if the file is missing it could cause GMaven to treat the source as inline instead.

URL

URL sources allow the script to be resolved by a URL, which can be any URL which the JVM can resolve.

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

or:

mvn groovy:execute -Dsource=http://mydomain.com/myscript.groovy

Inline

Inline allows scripts to be defined inside of project files.

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>generate-resources</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>
            println 'Hello'
          </source>
        </configuration>
      </execution>
  </executions>
</plugin>

or:

mvn groovy:execute -Dsource="println 'Hello'"

Maven interpolation and GStrings

While this form may be the simplest to use for many cases, there are some pitfalls to be aware of. Specifically use of GStrings (special String-like constructs available to Groovy), can cause some unexpected behavior due to Maven interpolation syntax ${} overloading the GString syntax.

When Maven loads a project, it very early on, interpolates the content of the POM to replace properties and simple expressions with string values. This is very common and very handy, but it does provide some complication when Maven attempts to resolve an expression which was expected to be resolved by a GString.

Scripts which make heavy use of GStrings should seriously consider using a File-based source configuration to avoid this potential complication.

Properties and Defaults

Additional customization of the execution variable properties are available for scripts run with execute.

Script executions can have overriding properties by setting the properties configuration parameter. Values set here will take precendence over any other property definition (user, system, project, defaults, etc).

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>generate-resources</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <properties>
            <name>Xenu</name>
          </properties>
          <source>
            println 'Hello ' + properties['name']
          </source>
        </configuration>
      </execution>
  </executions>
</plugin>

This example will always print Hello Xenu even if mvn -Dname=Steve or if the project defined a property of the same name.

Script executions can also have default properties by setting the defaults configuration parameter. Values set here will only be used if no other property for the same nme definition exists.

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
  <executions>
    <execution>
      <phase>generate-resources</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <defaults>
          <name>Xenu</name>
        </defaults>
        <source>
          println 'Hello ' + properties['name']
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>

Here, with out any other definition of name, will print Hello Xenu, but if instead was invoked with mvn -Dname=Jason would print Hello Jason instead.