gpt4 book ai didi

java - Maven 编译器未将 getters/setters(使用 Lombok 生成)添加到构建 jar 文件中

转载 作者:行者123 更新时间:2023-12-01 17:46:08 25 4
gpt4 key购买 nike

我有下面的 Pojo 类,我添加了来自 Lombok@Data

该项目在 Eclipse IDE 中运行良好,我什至可以在 IDE 的大纲窗口中看到 getter/setter。但是,当我从 IDE 运行 mvn clean installRun As Maven Install 时,生成的 jar 文件没有任何错误,但没有方法(getters settersequalshashcode)由Lombok生成。 outline window

既然IDE正在显示方法,那么POM文件中的maven编译器插件肯定存在一些问题。我尝试了 SO 中的所有可能的解决方案,但没有任何效果。

这是 POM:

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-boot</name>
<description>Demo project</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

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

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.0.Final</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>

</build>


</project>

我已将测试项目推送到Github 。可以从这里克隆:https://github.com/Puspendert/test-lambok.git

更新:为了验证 getter/setter 的生成是否会产生任何影响,我更改了 main 方法:

@SpringBootApplication
public class TestBootApplication {

public static void main(String[] args) {
User user = new User();
user.setName("hero");
System.out.println(user.getName());

}
}

然后,正如预期的那样,该项目在 mvn clean install 上出现编译错误:

[INFO] Building test-boot 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/puspender/git/test-lambok/test-boot/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/puspender/git/test-lambok/test-boot/src/main/java/com/example/TestBootApplication.java:[11,21] cannot find symbol
symbol: method setName(java.lang.String)
location: variable user of type com.example.User
[ERROR] /Users/puspender/git/test-lambok/test-boot/src/main/java/com/example/TestBootApplication.java:[12,40] cannot find symbol
symbol: method getName()
location: variable user of type com.example.User
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

请提出建议,这里可能出了什么问题?

最佳答案

您正在明确禁用注释处理!

<compilerArgument>-proc:none</compilerArgument>

删除这一行 - lombok 毕竟是一个注释处理库 - 并且所有内容(应该)工作文件

编辑:为了公平起见,还有另一个被删除的答案(出于某种原因),这也表明

关于java - Maven 编译器未将 getters/setters(使用 Lombok 生成)添加到构建 jar 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55247371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com