简体   繁体   English

Maven Groovy和Java + Lombok

[英]Maven Groovy and Java + Lombok

I am trying to add groovy to an existing Java Maven project that leverages Lombok. 我正在尝试将groovy添加到利用Lombok的现有Java Maven项目中。 Unfortunately when I enable the groovy-maven-eclipse compiler with the pom fragment below, my lombok annotated java files fail to compile. 不幸的是,当我使用下面的pom片段启用groovy-maven-eclipse编译器时,我的lombok注释的java文件无法编译。 As far as I can tell, Lombok is not participating in the compilation of java files at all. 据我所知,Lombok根本没有参与java文件的编译。

<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. 我还应该指出,在eclipse中(使用m2e)一切正常。 My problem arises when I try to do a mvn package. 当我尝试做一个mvn包时,我的问题出现了。

@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 ). @Todd:如果你不需要使用一些常规工具开发maven插件,那么groovy-eclipse-compiler是最好的选择(参见http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven )。

@Ambience: you reached the issue related at http://jira.codehaus.org/browse/GRECLIPSE-1293 . @Ambience:您在http://jira.codehaus.org/browse/GRECLIPSE-1293上遇到了相关问题。 This bug is now fixed with latest groovy-eclipse-compiler 2.6.1-01-SNAPSHOT. 现在用最新的groovy-eclipse-compiler 2.6.1-01-SNAPSHOT修复了这个bug。

Note : The latest version available is now 2.9.1-01 , see http://docs.groovy-lang.org/latest/html/documentation/tools-groovyeclipse.html 注意 :最新版本现在是2.9.1-01 ,请参阅http://docs.groovy-lang.org/latest/html/documentation/tools-groovyeclipse.html

You have to modify your pom like this: 你必须像这样修改你的pom:

<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 在maven-compiler-plugin中增加了对lombok的依赖

Edit: update versions 编辑:更新版本

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 (eg link to Codehaus), so I would basically have to rewrite it anyway just so as to update it. 我无意窃取这种声誉,但我也不想再次编辑它,因为它有点过时(例如链接到Codehaus),所以我基本上不得不重写它,以便更新它。

Here is a Maven POM based on 这是一个基于Maven的POM

  • Java 8 Java 8
  • Maven Compiler 3.7.0 Maven编译器3.7.0
  • Groovy 2.4.7 Groovy 2.4.7
  • Groovy Eclipse Compiler 2.9.3-01 Groovy Eclipse编译器2.9.3-01
  • Groovy Eclipse Batch 2.4.15-01 Groovy Eclipse Batch 2.4.15-01
  • Lombok 1.16.20 龙目岛1.16.20

It also contains the plugin repository configuration for the latest Groovy Eclipse version not found on Maven Central. 它还包含在Maven Central上找不到的最新Groovy Eclipse版本的插件存储库配置。

I am using this setup for my Spock + Geb tests, by the way. 顺便说一下,我正在使用这个设置进行Spock + Geb测试。

<?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 . 阅读有关此主题的更多信息在Groovy-Eclipse wikiLombok部分中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM