Pregunta

I am trying to add groovy to an existing Java Maven project that leverages Lombok. Unfortunately when I enable the groovy-maven-eclipse compiler with the pom fragment below, my lombok annotated java files fail to compile. As far as I can tell, Lombok is not participating in the compilation of java files at all.

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <compilerId>groovy-eclipse-compiler</compilerId>
        <verbose>true</verbose>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>2.6.0-01-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>

I should also point out that while in eclipse (with m2e) everything works fine. My problem arises when I try to do a mvn package.

¿Fue útil?

Solución

@Todd: The groovy-eclipse-compiler is the best choice if you don't need to developp maven plugin with some groovy tooling (see http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven).

@Ambience: you reached the issue related at http://jira.codehaus.org/browse/GRECLIPSE-1293. This bug is now fixed with latest groovy-eclipse-compiler 2.6.1-01-SNAPSHOT.

Note: The latest version available is now 2.9.1-01, see http://docs.groovy-lang.org/latest/html/documentation/tools-groovyeclipse.html

You have to modify your pom like this:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <compilerId>groovy-eclipse-compiler</compilerId>
      <verbose>true</verbose>   
      <fork>true</fork> 
      <compilerArguments>
        <javaAgentClass>lombok.launch.Agent</javaAgentClass>
      </compilerArguments>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>2.9.1-01</version>
        </dependency>
        <!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>2.3.7-01</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.4</version>
        </dependency>
    </dependencies>
</plugin>

The mandatory parts:

<fork>true</fork>

<compilerArguments>
    <javaAgentClass>lombok.launch.Agent</javaAgentClass>
</compilerArguments>

The added dependency on lombok inside the maven-compiler-plugin

Edit: update versions

Otros consejos

The correct answer at the time of writing it was and still is the accepted one. I have no intention of stealing that reputation, but I also do not want to edit it once more because it is kind of outdated (e.g. link to Codehaus), so I would basically have to rewrite it anyway just so as to update it.

Here is a Maven POM based on

  • Java 8
  • Maven Compiler 3.7.0
  • Groovy 2.4.7
  • Groovy Eclipse Compiler 2.9.3-01
  • Groovy Eclipse Batch 2.4.15-01
  • Lombok 1.16.20

It also contains the plugin repository configuration for the latest Groovy Eclipse version not found on Maven Central.

I am using this setup for my Spock + Geb tests, by the way.

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

  <groupId>de.scrum-master.testing</groupId>
  <artifactId>my-artifact</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <version.groovy-eclipse-compiler>2.9.3-01</version.groovy-eclipse-compiler>
    <version.groovy-eclipse-batch>2.4.15-01</version.groovy-eclipse-batch>

    <version.lombok>1.16.20</version.lombok>
  </properties>

  <pluginRepositories>
    <!-- Needed for latest Groovy Eclipse version -->
    <pluginRepository>
      <id>bintray</id>
      <name>Groovy Bintray</name>
      <url>https://dl.bintray.com/groovy/maven</url>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
          <!-- IMPORTANT -->
          <useIncrementalCompilation>false</useIncrementalCompilation>
          <encoding>${project.build.sourceEncoding}</encoding>
          <!-- Use Groovy Eclipse Compiler -->
          <compilerId>groovy-eclipse-compiler</compilerId>
          <!--
            Lombok agent needed for successful Maven compilation, see
            https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin#project-lombok
          -->
          <compilerArguments>
            <javaAgentClass>lombok.launch.Agent</javaAgentClass>
          </compilerArguments>
          <!-- Without this Lombok compilation fails -->
          <fork>true</fork>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>${version.groovy-eclipse-compiler}</version>
          </dependency>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>${version.groovy-eclipse-batch}</version>
          </dependency>
          <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${version.lombok}</version>
          </dependency>
        </dependencies>
      </plugin>

      <plugin>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-eclipse-compiler</artifactId>
        <version>${version.groovy-eclipse-compiler}</version>
        <extensions>true</extensions>
      </plugin>

    </plugins>
  </build>

  <dependencies>

    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.7</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${version.lombok}</version>
    </dependency>
  </dependencies>

</project>

Read more about this topic In the Lombok section of the Groovy-Eclipse wiki.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top