This documentation relates to an earlier version of Clover.
View

Unknown macro: {spacejump}

or visit the current documentation home.

Compiling Groovy with Groovy Eclipse Plugin

 

 

Bug Warning

Because of bug CLOV-1144 you have to keep your *.groovy files not in the location for Java code. If you put your Groovy sources into src/main/java or src/test/java (and this is unfortunately suggested solution on Groovy-Eclipse+compiler+plugin+for+Maven official page), you will end up with an error message like below:

 

[INFO] BUILD FAILURE
...
[ERROR] Failed to execute goal com.atlassian.maven.plugins:maven-clover2-plugin:3.1.7:setup (default-cli) 
...
Clover has failed to instrument the source files in the [C:\MyProject\target\clover\src-instrumented] directory at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217) ... [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException *** ERROR: No source files specified USAGE: com.cenqua.clover.CloverInstr [OPTIONS] PARAMS [FILES...] ...

 

There are several possible ways to configure Groovy Eclipse Plugin - see official http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven 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>

    as it will cause build error as described in bug above.

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 -->
            <!-- It works fine under Maven 3.x, but we've encountered problems with Maven 2.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>maven-clover2-plugin</artifactId>
                <version>3.1.7</version>
                <configuration>
                    <generateHtml>true</generateHtml>
                    <historyDir>.cloverhistory</historyDir>
                </configuration>
            </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 -->
            <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>maven-clover2-plugin</artifactId>
                <version>3.1.7</version>
                <configuration>
                    <generateHtml>true</generateHtml>
                    <historyDir>.cloverhistory</historyDir>
                </configuration>
            </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 clover2:setup install clover2:aggregate clover2: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 clover2:setup goal is called in the process-sources phase the latest.

References

 

  • No labels