Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Groovy and Java + Lombok

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.

like image 924
Steve Skrla Avatar asked Dec 15 '11 18:12

Steve Skrla


People also ask

Does Lombok work with Groovy?

Lombok is a java annotation processor which generates boilerplate code automatically in your classes at compile time. Many of lombok's feature overlap with features in the groovy language and AST Transformations.

What is Lombok maven?

Project Lombok is a mature java library that plugs into your editor or IDE like eclipse, STS, IntelliJ etc, also can plugs into build tools like maven, gradle, ant etc. Lombok library helps your ide to ignore generating boiler plate code for your pojo classes. And this is done during compile time.

What is Annotationprocessorpath?

<annotationProcessorPaths>If specified, the compiler will detect annotation processors only in those classpath elements. If omitted, the default classpath is used to detect annotation processors. The detection itself depends on the configuration of annotationProcessors.


2 Answers

@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

like image 70
Christophe Furmaniak Avatar answered Sep 21 '22 01:09

Christophe Furmaniak


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.

like image 23
kriegaex Avatar answered Sep 19 '22 01:09

kriegaex