-
Notifications
You must be signed in to change notification settings - Fork 36
Examples
The following examples describe some popular ways GMavenPlus can be used.
- Pure Groovy Compilation
- Joint Compilation
- InvokeDynamic Compilation
- Spock 1 and JUnit
- Spock 2 and JUnit
- Dynamic Properties
- Configuration Script
- Android Compilation
- Additional Sources
- GroovyDoc
- Add GroovyDoc to Site
- GroovyDoc jars
- Execute Scripts
- Groovy Console
- Groovy Shell
- Groovy Maven Plugins
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>3.0.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</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
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>3.0.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</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
Groovy 4.x and newer
Groovy 4.x and later only supports the invokedynamic bytecode so this section doesn't apply.
Groovy 2.x and 3.x
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>3.0.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<configuration>
<invokeDynamic>true</invokeDynamic>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<!-- any indy version of Groovy \>= 2.0.0-beta-3 should work here -->
<version>3.0.18</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
Spock 1 works with JUnit 4, you can add that to your project like this
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>joint-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.5.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.3-groovy-2.5</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*Spec.class</include>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Spock 2 uses JUnit 5, but you can also run JUnit 4 style tests by adding the appropriate dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>joint-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-bom</artifactId>
<version>2.1-groovy-3.0</version>
<!-- use below for Groovy 4 -->
<!-- <version>2.2-M1-groovy-4.0</version> -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<!-- add dependencies below to enable JUnit 4 style tests -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*Spec.class</include>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
You can define properties dynamically in the middle of the maven run so that later maven goals can react to that property. This only works for adding properties, updating an existing property will not work. This gives you Maven Plugin like capability without the overhead of setting up a separate plugin codebase. In this example, the version of the project is introspected and maven "deploy" like functionality is created for writing on SNAPSHOT or RELEASE docker registries. (from stackoverflow)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>parse-version</id>
<goals>
<goal>parse-version</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>3.0.2</version>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<executions>
<execution>
<id>add-dynamic-properties</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>
<![CDATA[
@Grapes([
@Grab(group='org.apache.commons', module='commons-lang3', version='3.3.2')
])
import java.text.SimpleDateFormat
Date now = new Date()
SimpleDateFormat timestamp = new SimpleDateFormat("yyyyMMdd.HHmmss")
myver = "${project.version}"
myqual = "${parsedVersion.qualifier}"
myrepo = (myqual == "SNAPSHOT") ? "${docker.repo.snapshot}" : "${docker.repo.release}"
mytag = (myqual == "SNAPSHOT") ? myver + "-" + timestamp.format(now) : myver
project.properties.setProperty('docker.repo.name', myrepo)
project.properties.setProperty('docker.image.tag', mytag)
log.info("Docker repository name is " + project.properties['docker.repo.name'])
log.info("Docker image tag is " + project.properties['docker.image.tag'])
]]>
</script>
</scripts>
</configuration>
</execution>
</executions>
</plugin>
To compile using a configuration script, you just need to tell GMavenPlus where your script is. To do that, add something like this to your pom.xml
:
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<configuration>
<configScript>config.groovy</configScript>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</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
To compile Groovy for use in Android, you need to include the Groovy jar that supports it. Everything else is the same as the normal compilation usages. To do this, you should add something similar to the following in your pom.xml
:
<project>
<properties>
<!-- you can use 1.7 if targeting KitKat (19) or higher -->
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<finalName>gmavenplus-plugin</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>4.6.0</version>
<extensions>true</extensions>
<configuration>
<sdk>
<!-- a list of version codes is available here: https://developer.android.com/reference/android/os/Build.VERSION_CODES.html -->
<platform>28</platform>
<!-- assumes ANDROID_HOME environment variable is set and SDK matching this version is installed there -->
</sdk>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</version>
<classifier>grooid</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
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>3.0.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</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.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</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
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>3.0.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</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 Java 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>
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>3.0.2</version>
<executions>
<execution>
<phase>site</phase>
<goals>
<goal>generateStubs</goal>
<goal>generateTestStubs</goal>
<goal>groovydoc</goal>
<goal>groovydocTests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</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)
GMavenPlus can create jar archives and bind them to the project. 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>3.0.2</version>
<executions>
<execution>
<phase>site</phase>
<goals>
<goal>generateStubs</goal>
<goal>generateTestStubs</goal>
<goal>groovydoc-jar</goal>
<goal>groovydocTests-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</version>
</dependency>
</dependencies>
</project>
By default, these goals will also invoke groovydoc
/groovydocTests
goals. If you've already generated the GroovyDoc previously in the lifecycle, you can skip this by setting invokeGroovyDoc
to false
.
Also 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)
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>
<properties>
<projectProp>yetAnotherValue</projectProp>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>execute</id>
<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("projectProp is " + project.properties['projectProp'])
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>
<script>${project.basedir}/src/main/resources/groovyScripts/someOtherScript.groovy</script>
</scripts>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.15</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
To execute your script(s), simply execute GMavenPlus's execute goal:
mvn gplus:execute@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>
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>3.0.2</version>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-console</artifactId>
<version>4.0.15</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
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>3.0.2</version>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-groovysh</artifactId>
<version>4.0.15</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
An example of how to use GMavenPlus to create Maven plugins in Groovy can be seen in GMavenPlus's own integration test of that functionality here.