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

    • build-helper-maven-plugin to define additional source roots (Maven 2)

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.6</source>
                    <target>1.6</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>com.atlassian.maven.plugins</groupId>
                <artifactId>clover-maven-plugin</artifactId>
                <version>4.1.1</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

 

Maven 2 POM

Build life cycle extension (used by groovy-eclipse-compiler) is not supported in Maven 2.x. Therefore, you can add source locations via build-helper-maven-plugin.

Maven 2 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-maven2-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Groovy Eclipse Plug-in Sample for Maven 2</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.6</source>
                    <target>1.6</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>
            <!-- Use Build Helper plugin which adds new source folders for Groovy, without modifying build cycle -->
            <!-- (as groovy-eclipse-compiler extensions="true" does). Thanks to this, Clover will be able -->
            <!-- to find your Groovy sources. Works for Maven 2.x and 3.x -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/groovy</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-source</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/test/groovy</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </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>com.atlassian.maven.plugins</groupId>
                <artifactId>clover-maven-plugin</artifactId>
                <version>4.1.1</version>
            </plugin>
        </plugins>
    </build>
</project>

 

In the build log you'll find messages like:

[INFO] [build-helper:add-source {execution: add-source}]
[INFO] Source directory: c:\MyProject\src\main\groovy added.
...
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files


[INFO] [build-helper:add-test-source {execution: add-test-source}]
[INFO] Test Source directory: c:\MyProject\src\test\groovy added.
...
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files

 

Build Command

Run your build with Clover using a following command (Maven 2 & 3):

mvn clean clover:setup install clover:aggregate clover:clover

 

Tips

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

 

Troubleshooting

Clover 3.1.11 and older: because of bug CLOV-1144 - Getting issue details... STATUS  (it was fixed in Clover 3.1.12) you can't keep your *.groovy files in src/main/java or src/test/java folders.

References

Code samples

Checkout sources of the clover-maven-plugin from Bitbucket:

hg clone https://bitbucket.org/atlassian/maven-clover2-plugin

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

 

Last modified on Jan 16, 2017

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.