【问题标题】:Maven compiler not adding getters/setters(generated using Lombok) to the build jar fileMaven 编译器未将 getter/setter(使用 Lombok 生成)添加到构建 jar 文件中
【发布时间】:2019-03-20 04:59:36
【问题描述】:

我在下面的 Pojo 类中添加了来自 Lombok@Data

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

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

请指教,这里有什么问题?

【问题讨论】:

  • 你在 Eclipse 中安装了 lombok 插件和东西,对吧?我知道 IntelliJ 开箱即用地支持它,但我上次在 Eclipse 中尝试时,您必须手动安装。

标签: java maven lombok


【解决方案1】:

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

&lt;compilerArgument&gt;-proc:none&lt;/compilerArgument&gt;

删除这一行 - lombok 毕竟是一个注释处理库 - 一切(应该)工作文件

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

【讨论】:

  • 谢谢。有效。实际上我从某个地方复制了编译器插件代码。请问-proc:none是什么意思?
  • 简而言之,它告诉编译器忽略lombok等注释处理工具
猜你喜欢
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
相关资源
最近更新 更多