Skip to end of metadata
Go to start of metadata

Examples

The following examples describe the various ways GMavenPlus can be used.

 

Pure Groovy Compilation

To compile Groovy code as part of the project compilation when there are no dependencies between Java and Groovy, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <!-- any version of Groovy \>= 1.5.0 should work here -->
      <version>2.3.7</version>
    </dependency>
  </dependencies>
</project>

To compile both the main and test Groovy sources, simply execute the normal compile phase from the default lifecycle:

mvn compile

 

Joint Compilation

To compile projects where you have mixed Java and Groovy (with dependencies between the two), you'll need to generate stubs in addition to normal compilation. To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>addSources</goal>
              <goal>addTestSources</goal>
              <goal>generateStubs</goal>
              <goal>compile</goal>
              <goal>testGenerateStubs</goal>
              <goal>testCompile</goal>
              <goal>removeStubs</goal>
              <goal>removeTestStubs</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <!-- any version of Groovy \>= 1.8.2 should work here -->
      <version>2.3.7</version>
    </dependency>
  </dependencies>
</project>

This will modify Maven's source directories to contain both the Java sources and the Groovy sources, but not the Java stubs (these were removed by the removeStubs and removeTestStubs goals). If you need to keep them (for example for polyglot builds), don't call those goals until after you are are done using the stubs.

To compile both the main and test Groovy and Java sources, simply execute the normal compile phase from the default lifecycle:

mvn compile

InvokeDynamic Compilation

To take advantage of invokedynamic, you need to include the Groovy jar that supports it and set the configuration option. If you only include the Groovy indy jar without setting the configuration option only Groovy's classes will take advantage of invokedynamic, your classes will not. To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <invokeDynamic>true</invokeDynamic>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <!-- any indy version of Groovy \>= 2.0.0-beta-3 should work here -->
      <version>2.3.7</version>
      <classifier>indy</classifier>
    </dependency>
  </dependencies>
</project>

To compile both the main and test Groovy sources, simply execute the normal compile phase from the default lifecycle:

mvn compile

 

Additional Sources

If you have sources that don't have the default extension (.groovy) or are in non-default directories (src/main/groovy and src/test/groovy) you must configure GMavenPlus to recognize those sources. To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <!-- this example has a structure of
               ./
               |-- src/
                   |-- main/
                       |-- groovy/
                           |-- Class1.groovy
                   |-- additional/
                       |-- groovy/
                           |-- Class2.groovy
                       |-- gvy/
                           |-- Class3.gvy
                   |-- test/
                       |-- groovy/
                       |-- Class1Test.groovy
                   |-- additional/
                       |-- groovy/
                           |-- Class2Test.groovy
                       |-- gvy/
                           |-- Class3Test.gvy
          -->
          <sources>
            <source>
              <directory>${project.basedir}/src/main/groovy</directory>
              <includes>
                <include>**/*.groovy</include>
              </includes>
            </source>
            <source>
              <directory>${project.basedir}/src/additional</directory>
              <includes>
                <include>groovy/**/*.groovy</include>
                <include>gvy/**/*.gvy</include>
              </includes>
            </source>
          </sources>
          <testSources>
            <testSource>
              <directory>${project.basedir}/src/test/groovy</directory>
              <includes>
                <include>**/*.groovy</include>
              </includes>
            </testSource>
            <testSource>
               <directory>${project.basedir}/src/additionalTest</directory>
              <includes>
                <include>groovy/**/*.groovy</include>
                <include>gvy/**/*.gvy</include>
              </includes>
            </testSource>
          </testSources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <!-- any version of Groovy \>= 1.5.0 should work here -->
      <version>2.3.7</version>
    </dependency>
  </dependencies>
</project>

To compile both the main and test Groovy sources, simply execute the normal compile phase from the default lifecycle:

mvn compile

 

Groovydoc

To generate Groovydoc for mixed Groovy and Java projects manually, you only need add the plugin (no need to configure any executions). To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <!-- any version of Groovy \>= 1.5.0 (except 1.6 RC 1) should work here -->
      <version>2.3.7</version>
    </dependency>
  </dependencies>
</project>

To generate the groovydoc for the main sources, simply execute GMavenPlus's groovydoc goal:

mvn gplus:groovydoc

If you are doing this for Groovy objects that also use Jav sources, you will also need to execute GMavenPlus's generateStubs goal in addition to the groovydoc goal:

mvn gplus:generateStubs gplus:groovydoc

To make this easy to invoke (like above), you may want to add the plugin group to your settings.xml:

<pluginGroups>
  <pluginGroup>org.codehaus.gmavenplus</pluginGroup>
<pluginGroups>

 

Add Groovydoc to Site

Note that this mojo is not set up to be used in the <reporting> section. So you will need to set the <outputDirectory> and <executions> to mimic this functionality. To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <phase>site</phase>
            <goals>
              <goal>generateStubs</goal>
              <goal>testGenerateStubs</goal>
              <goal>groovydoc</goal>
              <goal>testGroovydoc</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <!-- any version of Groovy \>= 1.8.2 should work here -->
      <version>2.3.7</version>
    </dependency>
  </dependencies>
</project>

To generate the groovydoc for the main sources, simply execute the normal site phase from the default lifecycle:

mvn site

Keep in mind that you can exclude the stub generation goals from the execution if

  • You don't have any Java that Groovy classes use
  • Another execution has already generated the stubs (typical in site lifecycle)

 

Execute Scripts

Note that if you'd like to separate script dependencies from those of your project, you can use Groovy's @Grab annotation instead of making them plugin dependencies if you prefer. Any plugin dependencies and any project test dependencies are also available for use in your scripts (remember that test scope also includes compile scope). This example demonstrates all three ways of including dependencies. Note that Maven will try to filter dollar curly references, so you'll need to either make sure the names don't collide or use the dollar without the curly if you don't want that to happen. Note that you can also add your own properties to use in the script (that can be set from the POM's properties or the command line. To execute Groovy scripts in build, add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <properties>
            <property>
              <name>someProp</name>
              <value>${someProp}</value>
            </property>
          </properties>
          <scripts>
            <script><![CDATA[
              @Grapes([
                @Grab(group='org.apache.commons', module='commons-lang3', version='3.3.2')
              ])
              import org.apache.commons.lang3.SystemUtils
              log.debug("The settings are " + session.settings)
              log.debug("This session's goals are " + session.goals)
              log.debug("The local repository is " + session.localRepository)
              log.debug("The reactor projects are " + session.sortedProjects)
              log.debug("The plugin artifacts are " + pluginArtifacts)
              log.debug("The mojo execution is " + mojoExecution)
              log.debug("The plugin descriptor is " + mojoExecution.mojoDescriptor)
              log.debug("someProp is " + someProp)
              log.debug("Using Java " + SystemUtils.JAVA_VERSION)
              assert ant.project.baseDir == project.basedir
              // the first reference is not filtered by Maven, the second reference is
              assert "$project.name" == "${project.name}"
            ]]></script>
            <script>file:///${project.basedir}/src/main/resources/groovyScripts/someScript.groovy</script>
          </scripts>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>2.3.7</version>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <!-- this is here for both the script and the project to use -->
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.0.13</version>
    </dependency>
  </dependencies>
</project>

To execute your script(s), simply execute GMavenPlus's execute goal:

mvn gplus:execute

To make this easy to invoke (like above), you may want to add the plugin group to your settings.xml:

<pluginGroups>
  <pluginGroup>org.codehaus.gmavenplus</pluginGroup>
<pluginGroups>

 

Groovy Console

If you're already using GMavenPlus in your project, no additional configuration is needed. If you're not already using GMavenPlus in your project but want to be able to use the console, you'll need to define the version of GMavenPlus and Groovy. To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>2.3.7</version>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

To launch a Groovy console bound to the current project, simply execute GMavenPlus's console goal:

mvn gplus:console

Or you can start a console without having GMavenPlus in your project, as long as you have a dependency on Groovy in your project by running the complete path to the goal:

mvn org.codehaus.gmavenplus:gmavenplus-plugin:1.2:console

 

Groovy Shell

If want to be able to use the shell, you'll need to define the version of GMavenPlus, Groovy, and JLine. To do this, you should add something similar to the following in your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.2</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>2.3.7</version>
            <scope>runtime</scope>
          </dependency>
          <!-- if you are using a version of Groovy \< 2.2.0-beta-1, you'll need to uncomment below -->
          <!--<dependency>-->
            <!--<groupId>jline</groupId>-->
            <!--<artifactId>jline</artifactId>-->
            <!--<version>1.0</version>-->
            <!--<scope>runtime</scope>-->
          <!--</dependency>-->
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

To launch a Groovy console bound to the current project, simply execute GMavenPlus's shell goal:

mvn gplus:shell
  • No labels