Lombok 没有在 Maven 中编译

Lombok not compiling in maven

我正在尝试通过 maven 编译使用 lombok 的项目,但遇到以下问题:

[INFO] ------------------------------------------------------------------------
[INFO] Building akka-calculator-lib 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ akka-calculator-lib ---
[INFO] Deleting C:\dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ akka-calculator-lib ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ akka-calculator-lib ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 11 source files to C:\dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\operation\multiply\MultiplyResult.java:[13,22] error: variable firstNumber not initialized in the default constructor
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\operation\multiply\MultiplyResult.java:[14,22] error: variable secondNumber not initialized in the default constructor
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\operation\multiply\MultiplyResult.java:[15,22] error: variable result not initialized in the default constructor
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[35,52] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[35,87] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[36,54] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[37,74] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[38,74] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[38,101] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[43,50] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[43,85] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[44,64] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[45,84] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[46,84] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[46,111] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[51,50] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[51,85] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[52,64] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[53,84] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[54,84] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[54,111] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[59,50] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[59,85] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[60,60] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[61,80] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[62,89] error: cannot find symbol
[ERROR] \dev\projects\Akka\akka-testing\akka-calculator\akka-calculator-lib\src\main\java\com\mangusbrother\akka\calculator\lib\CalculatorActor.java:[62,127] error: cannot find symbol
[INFO] 27 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] akka-parent ........................................ SUCCESS [  0.164 s]
[INFO] akka-testing ....................................... SUCCESS [  0.011 s]
[INFO] akka-calculator .................................... SUCCESS [  0.010 s]
[INFO] akka-calculator-lib ................................ FAILURE [  2.758 s]
[INFO] akka-calculator-creation ........................... SKIPPED
[INFO] akka-calculator-lookup ............................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.120 s
[INFO] Finished at: 2016-02-14T19:01:21+01:00
[INFO] Final Memory: 14M/169M
[INFO] ------------------------------------------------------------------------

第一个错误类型是针对此 class 中的字段:

package com.mangusbrother.akka.calculator.lib.operation.multiply;


import com.mangusbrother.akka.calculator.lib.operation.MathematicalResult;
import lombok.Data;

/**
 * Created by Mangusbrother on 14/02/2016.
 */
@Data
public class MultiplyResult implements MathematicalResult {
    private static final long serialVersionUID = 738641818941986L;
    private final int firstNumber;
    private final int secondNumber;
    private final int result;
}

第二个错误是 class 试图从 lombok classes

访问 getter

我的 maven pom.xml 中有以下内容:

<properties>
    <lombok.version>1.16.6</lombok.version>
</properties>
<dependencies>
   <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

首先,您应该将您的 lombok 源代码(java 用 lombok 注释)定位到 src/main/lombok 并将 lombok-maven-plugin 添加到您的项目中,如下所示:

<build>
  <plugins>
    <plugin>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok-maven-plugin</artifactId>
      <version>1.16.6.1</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>delombok</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

如果你不喜欢使用 lombok-maven-plugin,你需要添加以下内容:

<dependencies>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.6</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

如果您真的喜欢将您的 lomboked 源放入 src/main/java 而不是默认的 /src/main/lombok you have to go this way,这是不推荐的:

<build>
  <sourceDirectory>target/generated-sources/delombok</sourceDirectory>
  <testSourceDirectory>target/generated-test-sources/delombok</testSourceDirectory>
  <plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.16.6.1</version>
    <executions>
      <execution>
        <id>delombok</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>delombok</goal>
        </goals>
        <configuration>
          <addOutputDirectory>false</addOutputDirectory>
          <sourceDirectory>src/main/java</sourceDirectory>
        </configuration>
      </execution>
      <execution>
        <id>test-delombok</id>
        <phase>generate-test-sources</phase>
        <goals>
          <goal>testDelombok</goal>
        </goals>
        <configuration>
          <addOutputDirectory>false</addOutputDirectory>
          <sourceDirectory>src/test/java</sourceDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</build>

我不知道为什么,但是由于 maven-compiler-plugin,您遇到了编译器错误。如果可以,请将插件版本更改为 3.5 并检查它是否可以工作。

我为此苦苦挣扎并得出结论,Lombok 1.16+Java 8 是早期版本的问题Maven 的编译器插件。 IDEA 2017.1 构建代码时没有错误,但 Maven 在 public 方法上抛出 'symbol not found' 错误,这些方法肯定存在于反编译的 jar 文件中。

通过在我的项目及其库中升级到 maven-compiler-plugin 3.6.1真的很想避免。

annotationProcessorPaths 添加到您的 maven-compiler-plugins 配置中:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>${maven-compiler-plugin.version}</version>
  <configuration>
    <annotationProcessorPaths>
      <path>
        <groupId>org.projectlombok</groupId>     
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </path>
    </annotationProcessorPaths>
  </configuration>
</plugin>

您还需要检查路径中 java 的版本。我的问题是 Java 1.8 & 3.5 版本。为了让它工作,我必须切换到 1.7 & 3.5 。请注意我们还在 1.7

尽管 OP 的问题似乎已经解决,但这些症状也可能由此引起:

如果 Lombok 在您的 IDE 中工作,但在 Maven 构建中表现得像它不生成任何方法等,原因可能是 Maven 已设置为使用与 javac 编译器不同的编译器,可能有不同的说明。

例如,如果您的 POM(或父 POM)包含如下内容:

    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerId>eclipse</compilerId>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

在那种情况下,使用的是 eclipse 编译器,这个编译器对 lombok 的友好性要差得多,has special instructions

[对于我的案例来说,构建过程太扭曲了,结果我们决定不使用 lombok,遗憾的是]

我的工作配置如下。

在 pom 文件中:

     <properties>
        <java.version>14</java.version>
        <lombok-version>1.18.12</lombok-version>
    </properties>

     <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok-version}</version>            
            <optional>true</optional>
        </dependency>

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>14</source>
                    <target>14</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok-version}</version>
                        </path>
                    </annotationProcessorPaths>                 
                </configuration>
            </plugin>
        </plugins>
    </build>

Maven 编译器插件版本 3.8.1 在这里很重要,因为旧版本不适合我。

在module-info.java

 requires static lombok;

使用此参数增加 JVM 的堆栈大小:-Xss10240m

MAVEN_OPTS

set MAVEN_OPTS=-Xss10240m"export MAVEN_OPTS="-Xss10240m".

_JAVA_OPTIONS

_JAVA_OPTIONS="-Xss10240m".

maven-compiler-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <compilerArgs>
            <arg>-Xss10240m</arg>
        </compilerArgs>
    </configuration>
</plugin>

spring-boot-maven-plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>
            -Xss10240m
        </jvmArguments>
    </configuration>
</plugin>