简体   繁体   English

Lombok 未在 maven 中编译

[英]Lombok not compiling in maven

I am trying to compile my project that uses lombok through maven and i am getting the following issue:我正在尝试通过 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] ------------------------------------------------------------------------

Where the first error type is for the fields in this 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;
}

and the second error is when a class tries to access getters from lombok classes第二个错误是当一个类尝试从 lombok 类访问 getter 时

My maven pom.xml has the following in it:我的 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>

Add annotationProcessorPaths to your maven-compiler-plugins configurations: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>

I don't know why but you got compiler error because of maven-compiler-plugin.我不知道为什么,但是由于 maven-compiler-plugin,您遇到了编译器错误。 If you can please change plugin version to 3.5 and check if it will work.如果可以,请将插件版本更改为 3.5 并检查它是否可以工作。

I struggled mightily with this and concluded that Lombok 1.16+ and Java 8 are a problem with earlier versions of Maven's compiler plugin.我为此苦苦挣扎并得出结论, Lombok 1.16+Java 8是早期版本的 Maven 编译器插件的问题。 IDEA 2017.1 was building the code without errors, but Maven was throwing 'symbol not found' errors on public methods that were definitely there in a decompiled jar file. IDEA 2017.1 正在构建没有错误的代码,但 Maven 在公共方法上抛出“找不到符号”错误,这些错误肯定存在于反编译的 jar 文件中。

By upgrading to maven-compiler-plugin 3.6.1 across my project and its libraries I was able to get it resolved without having to go down the Delombok path, which I really wanted to avoid.通过在我的项目及其库中升级到maven-compiler-plugin 3.6.1 ,我能够解决它,而不必走我真正想避免的 Delombok 路径。

My working configuration is as below.我的工作配置如下。

In the pom file:在 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 compiler plugin version 3.8.1 is significant here as the older version didn't work for me. Maven 编译器插件版本 3.8.1 在这里很重要,因为旧版本对我不起作用。

In the module-info.java在模块-info.java

 requires static lombok;

First you should locate your lombok source (java annotated with lombok) into src/main/lombok and add the lombok-maven-plugin to your project like this:首先,您应该将 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>

If you don't like to use the lombok-maven-plugin you need to add the following:如果您不喜欢使用 lombok-maven-plugin,则需要添加以下内容:

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

If you really like to put your lomboked source into src/main/java instead of the default /src/main/lombok you have to go this way which is not recommended:如果你真的想把你的 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>

Although the OP's problem appears to have been solved these symptoms can also be caused by this:虽然 OP 的问题似乎已经解决,但这些症状也可能是由以下原因引起的:

If Lombok is working in your IDE but behaving like its not generating any methods etc in the Maven build a cause of this can be that Maven has been set to use a different compiler to the javac compiler, which may have different instructions.如果 Lombok 在您的 IDE 中工作,但表现得像它没有在 Maven 构建中生成任何方法等,原因可能是 Maven 已设置为使用与 javac 编译器不同的编译器,这可能具有不同的指令。

For example if your POM (or parent POM) contains something like this:例如,如果您的 POM(或父 POM)包含如下内容:

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

In that case the eclipse compiler is being used, this compiler is much less lombok friendly and has special instructions .在这种情况下,将使用 eclipse 编译器,此编译器对 lombok 不那么友好,并且具有特殊说明

[For my case that was bending the build process too out of shape and we decided not to use lombok as a result, sadly] [对于我的情况,构建过程太变形,因此我们决定不使用 lombok,可悲的是]

You will also need to check what version of java is there in path .您还需要检查 path 中的 java 版本。 My issue was with Java 1.8 & 3.5 version .我的问题是 Java 1.8 & 3.5 版本。 To get it working I had to switch to 1.7 & 3.5 .为了让它工作,我不得不切换到 1.7 & 3.5 。 Please note we are still on 1.7请注意我们仍然在 1.7

Increase the stack size of the JVM using this argument: -Xss10240m使用此参数增加 JVM 的堆栈大小: -Xss10240m

MAVEN_OPTS MAVEN_OPTS

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

_JAVA_OPTIONS _JAVA_OPTIONS

_JAVA_OPTIONS="-Xss10240m" . _JAVA_OPTIONS="-Xss10240m"

maven-compiler-plugin 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 spring-boot-maven-plugin

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

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

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