【问题标题】:Maven build failed because of Lambok annotated classes由于 Lambok 注释类,Maven 构建失败
【发布时间】:2019-03-19 10:04:35
【问题描述】:

我的项目是一个Spring-boot 2.1.1.RELEASE 应用程序。 这是一个多模块的 Maven 项目。这是父 POM 文件

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

    <name>example-parent</name>
    <description>example.com parent module</description>

    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>example-common</module>
        <module>example-persistence</module>
        <module>example-rest</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>

        </plugins>

        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                    <configuration>
                        <source>${java-version}</source>
                        <target>${java-version}</target>
                        <compilerArgument>-proc:none</compilerArgument>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>

    </build>

    <properties>
        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
        <java-version>1.8</java-version>
    </properties>

</project>

example-common 对 getter 和 setter 使用 lambok 注释。这里是example-common的POM文件

<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>com.example</groupId>
        <artifactId>example-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>example-common</artifactId>
    <name>example-common</name>
    <description>module contains commons utils for all other modules</description>

    <dependencies>

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

        <!-- other dependencies -->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <compilerArgument>-proc:none</compilerArgument>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

如您所见,我使用的是&lt;annotationProcessorPaths&gt;

@Data
public class CaptchaConfigs {

    private String site;
    private String secret;

}

@Data 注解在maven build 过程中没有生成getter/setter,因此报错

cannot find symbol [ERROR] symbol:   method getSite()
cannot find symbol [ERROR] symbol:   method getSecret()

getter/setter 存在于 IDE 中,我可以在大纲窗口中看到它们。

如果我删除 @Data 并手动编写 getter/setter,则 BUILD 成功。

我已经为lambok 完成了所有配置(lambok-xxx.jar),这就是为什么它没有在 IDE 中给出编译错误,它只在 BUILD 时失败。

我在 POM 做错了吗?

PS:我使用的是eclipse STS版本:3.9.6.RELEASE

【问题讨论】:

    标签: java eclipse maven


    【解决方案1】:

    选择第三个选项,如果不起作用,请尝试以下链接可能会有所帮助

    answer somehow related to your issue click on the link

    【讨论】:

    • 已经试过了。如果 IDE 未生成方法,您提供的链接具有解决方案。但就我而言,我可以在大纲窗口中看到 getter/setter。它们不仅在构建时存在。在问题中添加了图片。
    【解决方案2】:

    请将maven-compiler-plugin 版本定义为3.5。使用 maven 编译器 3.5.x annotationProcessorPaths,工作正常。

    尝试将以下内容添加到您的 maven 编译器插件中。

    <version>3.5.1</version>
    

    【讨论】:

      【解决方案3】:

      我在同一个 Spring Boot 2.1.1.RELEASE 上使用 Lombok。我认为问题出在您的 Maven 插件上。以下是我的依赖项和插件,效果很好。

      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
      </dependency>
      
      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      

      【讨论】:

        【解决方案4】:

        正如我所料,这是我的配置。从这里得到答案。

        Maven compiler not adding getters/setters(generated using Lombok) to the build jar file

        实际上是&lt;compilerArgument&gt;-proc:none&lt;/compilerArgument&gt; 导致了这个问题。删除它解决了这个问题。

        【讨论】:

          猜你喜欢
          • 2012-05-27
          • 2017-09-01
          • 2020-01-16
          • 1970-01-01
          • 2017-08-14
          • 1970-01-01
          • 2021-05-30
          • 1970-01-01
          • 2018-02-21
          相关资源
          最近更新 更多