Lombok 未在 Maven 中编译

问题描述 投票:0回答:9

我正在尝试通过 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] ------------------------------------------------------------------------

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

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;
}

第二个错误是当一个类尝试从 lombok 类访问 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>
java maven lombok
9个回答
59
投票

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>

19
投票

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


8
投票

我的工作配置如下。

在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 在这里很重要,因为旧版本不适合我。

在模块-info.java中

 requires static lombok;

7
投票

我为此苦苦挣扎,得出的结论是 Lombok 1.16+Java 8 是 Maven 编译器插件早期版本的问题。 IDEA 2017.1 构建代码没有错误,但 Maven 在公共方法上抛出“符号未找到”错误,而这些方法肯定存在于反编译的 jar 文件中。

通过在我的项目及其库中升级到 maven-compiler-plugin 3.6.1,我能够解决这个问题,而不必沿着 Delombok 路径走下去,这是我真正想避免的。


1
投票

首先,您应该将 lombok 源(用 lombok 注释的 java)定位到

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
你必须走这条路,这是不推荐的:

<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>

1
投票

虽然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 不太友好并且 有特殊指令

[对于我的情况,构建过程过于弯曲,因此我们决定不使用 lombok,遗憾的是]


0
投票

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


0
投票

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

-Xss10240m

MAVEN_OPTS

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

_JAVA_选项

_JAVA_OPTIONS="-Xss10240m"
.

maven 编译器插件

<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>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>
            -Xss10240m
        </jvmArguments>
    </configuration>
</plugin>

0
投票

我使用的是带有arm64的macos,我也遇到这些错误,但这是由于我的pom.xml中的JDK版本比我的java版本高得多。 当安装与我的 pom 中的版本类似的版本(通过 sdkman)时,构建成功。希望对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.