无法使用 lombok 构建 maven jhipster 项目

Can't build maven jhipster project with lombok

./mvnw 和 mvn clean install 在添加 lombok 依赖项时失败,但在从 Intellij IDE 启动时 运行 成功 找到以下错误:

INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] src/main/java/web/rest/core/service/impl/ProductServiceImpl.java:[18,29] cannot find symbol
  symbol:   method builder()
  location: class com.test.one.web.rest.core.model.Product

这是 POJO

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class Product {
    private String name;
}

我设法使用以下版本组合:

  • maven-compiler-plugin: 3.3
  • 龙目岛:1.16.18(最新)

例如,pom.xml:

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

...

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

Jhipster生成的maven项目在maven编译插件中使用了annotationProcessorPaths,这就是为什么它无法查找最新的lombok,除非我们指定lombok作为注解处理器之一。

工作代码如下

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
    <annotationProcessorPaths>
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
        </path>
        <!-- For JPA static metamodel generation -->
        <path>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${hibernate.version}</version>
        </path>
        <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </path>
    </annotationProcessorPaths>
</configuration>