Versions Compared

Key

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

...

In your plugin section, you must change the compiler used by the maven-compiler-plugin. Like the javac ant task, the maven-compiler-plugin does not actually compile, but rather delegates the compilation to a different artifact (in our case, the groovy-eclipse-batch artifact):

Code Block
languagehtml/xml
<build>
...
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
      <compilerId>groovy-eclipse-compiler</compilerId>
      <!-- set verbose to be true if you want lots of uninteresting messages -->
      <!-- <verbose>true</verbose> -->
    </configuration>
    <dependencies>
      <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-eclipse-compiler</artifactId>
        <version>2.7.0-01</version>
      </dependency>
    </dependencies>
  </plugin>
  ...
</plugins>
</build>

By default, this will use Groovy 2.0.0 4 to compile your code. If you would prefer to use 1.8.6 or 1.7.10, then add another dependency to the maven-compiler-plugin:

Code Block
languagehtml/xml
<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-eclipse-batch</artifactId>
  <version>1.8.6-01</version>
  <!-- <version>1.7.10-06</version> -->
</dependency>

This will allow Groovy files to be compiled. The groovy-eclipse-compiler recognizes all settings supported by the maven-compiler-plugin.

Remember that you still need to specify a groovy artifact as a build dependency in addition to the maven-compiler-plugin dependency.  The groovy dependency version should match the compiler version.  Something like this:

Code Block
languagehtml/xml
<dependencies>
  ...
  <dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>1.8.6</version>
  </dependency>
  ...
</dependencies>

 

 

Note that the groovy-eclipse-compiler and groovy-eclipse-batch artifacts are available in Maven-central, so there is no need to explicitly declare any extra repositories.

...

If your project has no Java files and you don't want to add an empty file in src/main/java, then you can configure source files by referencing the groovy-eclipse-compiler mojo. Just add this to the plugins section of your pom:

Code Block
xml
languagehtml/xml
<build>
  ...
  <plugin>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-eclipse-compiler</artifactId>
    <version>2.7.0-01</version>
    <extensions>true</extensions>
  </plugin>
  ...
</build>

...

The build-helper-maven-plugin allows you to do things like adding extra source folders to your project without needing to redefine the default lifecycle. You need to add this configuration to your build plugin section:

Code Block
languagehtml/xml
<build>
  ...
  <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>
  ...
</build>

...

There is an archetype available for this project. You can use it through a command like this:

Code Block
languagebash
mvn archetype:generate \
    -DarchetypeGroupId=org.codehaus.groovy \
    -DarchetypeArtifactId=groovy-eclipse-quickstart \
    -DarchetypeVersion=2.5.2-01 \
    -DgroupId=foo \
    -DartifactId=bar \
    -Dversion=1 \
    -DinteractiveMode=false \
    -DarchetypeRepository=https://nexus.codehaus.org/content/repositories/snapshots/

...

Project Lombok is compatible with the groovy-eclipse-compiler.  There is some extra configuration that you need to do.  The lombok jar needs to be added to both the build and compile dependencies sections:

Code Block
languagehtml/xml
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>0.10.4</version>
</dependency>

Also, the following configuration needs to be added to the maven-compiler-plugin configuration:

Code Block
languagehtml/xml
<configuration>
  <compilerId>groovy-eclipse-compiler</compilerId>
  <verbose>true</verbose>
  <compilerArguments>
    <javaAgentClass>lombok.core.Agent</javaAgentClass>
  </compilerArguments>
  <fork>true</fork>
</configuration>

...