Compiling Groovy with Groovy Eclipse Plugin

Compiling Groovy with Groovy Eclipse Plugin

There are several possible ways to configure Groovy Eclipse Plugin - see official https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin page.

Our recommendation is to use configuration similar to the following:

Source layout

  • keep Java in src/main/java and src/test/java
  • keep Groovy in src/main/groovy and src/test/groovy
  • do not define Groovy source locations for maven-compiler-plugin directly, e.g.:
    <sourceDirectory>src/main/groovy</sourceDirectory>
    <testSourceDirectory>src/test/groovy</testSourceDirectory>
  • instead of this use:

    • extensions=true for groovy-eclipse-compiler (Maven 3) or

Maven 3 POM

Maven 3 pom.xml
<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>com.atlassian.samples</groupId>
    <artifactId>groovy-eclipse-plugin-maven3-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Groovy Eclipse Plug-in Sample for Maven 3</name>

    <!-- Dependencies for test execution and runtime -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>1.8.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <!-- Bind Groovy Eclipse Compiler -->
                    <compilerId>groovy-eclipse-compiler</compilerId>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <dependencies>
                    <!-- Define which Groovy version will be used for build (default is 2.0) -->
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>1.8.6-01</version>
                    </dependency>
                    <!-- Define dependency to Groovy Eclipse Compiler (as it's referred in compilerId) -->
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.7.0-01</version>
                    </dependency>
                </dependencies>
            </plugin>
            <!-- Define Groovy Eclipse Compiler again and set extensions=true. Thanks to this, plugin will -->
            <!-- enhance default build life cycle with an extra phase which adds additional Groovy source folders -->
            <!-- Thanks to this, Clover will be able to find your Groovy files. It works with Maven 3.x -->
            <plugin>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-eclipse-compiler</artifactId>
                <version>2.7.0-01</version>
                <extensions>true</extensions>
            </plugin>
            <!-- Configure Clover for Maven plug-in. Please note that it's not bound to any execution phase, -->
            <!-- so you'll have to call Clover goals from command line. -->
            <plugin>
                <groupId>org.openclover</groupId>
                <artifactId>clover-maven-plugin</artifactId>
                <version>4.5.0</version>
            </plugin>
        </plugins>
    </build>
</project>

 

In the build log you'll find messages like:

[INFO] --- groovy-eclipse-compiler:2.7.0-01:add-groovy-build-paths (default-add-groovy-build-paths)
@ groovy-eclipse-plugin-maven3-sample ---
[INFO] Adding /src/main/groovy to the list of source folders
[INFO] Adding /src/test/groovy to the list of test source folders

Tips

  • Note that it's possible to bind Clover goals to build phases using the <executions> tag in pom.xml. See Clover-for-Maven User's Guide, "Running goals via pom.xml" chapter. Just ensure that clover:setup goal is called in the process-sources phase the latest.

 

References

Code samples

Checkout sources of the clover-maven-plugin:

git clone https://github.com/openclover/clover-maven-plugin.git

Open the src/it directory. It contains a number of sample projects, including:

  • groovy-eclipse-plugin - shows an approach in which Groovy sources are stored in 'src/main/groovy' and 'src/test/groovy', while Java sources in 'src/main/java' and 'src/test/java'

  • groovy-eclipse-plugin-src-main-groovy - shows an approach where Groovy sources are placed in 'src/main/groovy' and 'src/test/groovy' folders, there are no Java sources, and <sourceDirectory> / <testSourceDirectory> options are used to redefine source roots
  • groovy-eclipse-plugin-src-main-java - shows an approach where Groovy source files are placed in 'src/main/java' and 'src/test/java' folders (these folders can contain Java sources as well), so that there is no need to define extra source folders via build-helper-maven-plugin