Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

There are several approaches to compiling Groovy code in your Maven projects. GMavenPlus tries to fill the role of the now discontinued GMaven and is the most flexible and feature rich, but it has some difficulties with joint Java-Groovy projects and it is no longer under active development-rich. The Groovy-Eclipse compiler plugin for Maven is a unique solution because it sidesteps the joint compilation issues. Read this page for a deeper discussion an overview of the benefits and disadvantages of the two approacheshow the choices in build tools differ.

A third approach is to use Maven's Ant plugin to compile a groovy project. Note that the Ant plugin is bound to the compile and test-compile phases of the build in the example below. It will be invoked during these phases and the contained tasks will be carried out which runs the Groovy compiler over the source and test directories. The resulting Java classes will coexist with and be treated like any standard Java classes compiled from Java source and will appear no different to the JRE , or the JUnit runtimeor any Java classes that interact with your Groovy classes.

Code Block
xml
xml
<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

   <groupId>com.mycomp.MyGroovy</groupId>
    <artifactId>MyGroovy</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Maven Example building a Groovy project</name>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
   <!-- This examples assumes your main Groovy is in src/main/groovy and your test Groovy is in src/test/groovy. -->
  <!-- The version of Groovy used in the example requires Java 6 or higher. -->
  <!-- This example won't work for Groovy that depends on Java or circular dependencies between Java and Groovy. -->

  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovyc-via-ant-test</artifactId>
  <name>test groovcy project</name>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <groovyVersion>2.3.2</groovyVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
     <groupId>groovy</groupId> <artifactId>groovy-all</artifactId>
      <version>${groovyVersion}</version>
     <artifactId>groovy <!-all-1.0-jsr</artifactId> Uncomment line below to use invokedynamic version of Groovy (requires Java 7 <version>05</version>
 or higher). -->
      </dependency>!--<classifier>indy</classifier>-->
    </dependencies>dependency>
  </dependencies>

<build>  <build>
      <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile<<phase>generate-sources</phase>
                        <configuration>
              <target>
             <tasks>                                 <mkdir dir="${basedir}/src/main/groovy"/>
                                <taskdef name="groovyc"                                     classname="org.codehaus.groovy.ant.Groovyc">
                                    <classpath refid="maven.compile.classpath" />
                                </taskdef>
                                <mkdir dir="${project.build.outputDirectory}" />
                <groovyc                <groovyc srcdir="${basedir}/src/main/groovy" destdir="${project.build.outputDirectory}"                                     srcdirencoding="${basedir}/src/main/groovy/" listfiles="true"project.build.sourceEncoding}">
                                    <classpath refid="maven.compile.classpath" />
                                </groovyc>
                            </tasks>target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile<<phase>generate-test-sources</phase>
                        <configuration>
              <target>
             <tasks>                                 <mkdir dir="${basedir}/src/test/groovy"/>
                                <taskdef name="groovyc"                                     classname="org.codehaus.groovy.ant.Groovyc">
                                    <classpath refid="maven.test.classpath" />
                                </taskdef>
                                <mkdir dir="${project.build.testOutputDirectory}" />
                <groovyc                <groovyc srcdir="${basedir}/src/test/groovy" destdir="${project.build.testOutputDirectory}"                                     srcdirencoding="${basedir}/src/test/groovy/" listfiles="true"project.build.sourceEncoding}">
                                    <classpath refid="maven.test.classpath" />
                                </groovyc>
                            </tasks>target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
            </goals>
           </goals>execution>
        </executions>
      </plugin>
    </execution>  <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
     </executions>   <version>3.1</version>
         </plugin>
        </plugins>
    </build>
</project>
</project>

This assumes you have a Maven project setup with "groovy" subfolders as peers to the java src and test subfolders. You can use the java/jar archetype to set this up then rename the java folders to groovy or keep the java folders and just create groovy peer folders. There exists, also a groovy plugin which has not been tested or used in production.   After defining the build section as in the above example, you can invoke the typical Maven build phases normally. For example, "mvn test" will execute the test phase, compiling Groovy source and Groovy test source and finally executing the unit tests. If you run "mvn jar" it will execute the jar phase bundling up all of your compiled production classes into a jar after all of the unit tests pass. For more detail on Maven build phases consult the Maven2 documentation.