简体   繁体   English

MapStruct 和 Lombok 不能一起工作

[英]MapStruct and Lombok not working together

Tech Stack being used:使用的技术堆栈:

Java 8 MapStruct: 1.2.0.Final Lombok: 1.16.18 IDE: IntelliJ - Lombok Plugin already installed Java 8 MapStruct:1.2.0.Final Lombok:1.16.18 IDE:IntelliJ - Lombok 插件已安装

  • Initially, I faced issues when I removed getters and setters and added @Getter and @Setter annotation, mapstruct is not able to find the property and says: Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?最初,当我删除 getter 和 setter 并添加@Getter@Setter注释时,我遇到了问题, mapstruct无法找到该属性并说: Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"? Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?
  • I came to know that Lombok 1.16.14 or newer along with MapStruct 1.2.0.Beta1 or newer are compatible and can work together, but my versions are newer then the desired still the issue is arising.我开始知道 Lombok 1.16.14 或更新版本以及 MapStruct 1.2.0.Beta1 或更新版本是兼容的并且可以一起工作,但是我的版本比所需版本更新,问题仍然出现。
  • One more solution that I have already tried is running Lombok's Delombok plugin, but still, the same issue is arising.我已经尝试过的另一种解决方案是运行 Lombok 的 Delombok 插件,但仍然会出现同样的问题。

Below are the project files:以下是项目文件:

The Entity Object: One.java :实体 Object: One.java

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class One {

    private int id;
    private Integer version;
    private int projectId;
    private String title;
    private String code;
    private int sortOrder;

}

The DTO Object: OneDTO.java : DTO Object: OneDTO.java

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class OneDto {

    private int id;
    private Integer version;
    private int projectId;
    private String title;
    private String code;
    private int sortOrder;

}

Mapper Class: OneMapper.java映射器 Class: OneMapper.java

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import com.vg.once.dto.OneDto;
import com.vg.once.entity.One;

@Mapper
public interface OneMapper {

    @Mapping(target="id", source="one.id")
    OneDto createOne (One one);

}

pom.xml pom.xml

<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>
  <groupId>com.vg</groupId>
  <artifactId>mapstruct</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Mapstruct-test</name>

    <properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
        <org.projectlombok.version>1.16.18</org.projectlombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${org.projectlombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins> 
         <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
                <version>1.16.18.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourceDirectory>src/main/java</sourceDirectory>
                    <addOutputDirectory>false</addOutputDirectory>
                    <outputDirectory>${project.build.directory}/delombok</outputDirectory>
                    <encoding>UTF-8</encoding>
                    <skip>false</skip>
                    <verbose>false</verbose>
                </configuration>
            </plugin>       
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>  
</project>

Build Trace :构建跟踪

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Mapstruct-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- lombok-maven-plugin:1.16.18.1:delombok (default) @ mapstruct ---
[INFO] Delombok complete.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mapstruct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ mapstruct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 5 source files to /home/vivekgupta/Documents/workspaces/mapstruct-test/mapstruct/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/vivekgupta/Documents/workspaces/mapstruct-test/mapstruct/src/main/java/com/vg/once/mapper/OneMapper.java:[12,9] Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.637 s
[INFO] Finished at: 2017-12-06T19:23:53+05:30
[INFO] Final Memory: 19M/235M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project mapstruct: Compilation failure
[ERROR] /home/vivekgupta/Documents/workspaces/mapstruct-test/mapstruct/src/main/java/com/vg/once/mapper/OneMapper.java:[12,9] Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

please share how can i get this working using both Lombok and MapStruct together?请分享我怎样才能同时使用 Lombok 和 MapStruct 使它工作?

The reason why it does not work is because Maven only uses the MapStruct processor and not the Lombok one.它不起作用的原因是因为 Maven 只使用 MapStruct 处理器而不是 Lombok 处理器。 The annotationProcessorPaths tells maven which processors it should use. annotationProcessorPaths告诉 maven 它应该使用哪些处理器。

The delombok does nothing as you are ending up with 2 files per class and I think that the maven compiler does not see them. delombok 什么都不做,因为你最终每个类有 2 个文件,我认为 maven 编译器看不到它们。

You have 2 options:您有 2 个选项:

Option 1: Add the lombok dependency in the annotationProcessorPaths选项 1:在annotationProcessorPaths中添加 lombok 依赖项

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${org.projectlombok.version}</version>
            </path>
            <!-- This is needed when using Lombok 1.18.16 and above -->
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.2.0</version>
            </path>
            <!-- Mapstruct should follow the lombok path(s) -->
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

Option 2:选项 2:

Add the mapstruct-processor dependency to your dependencies and remove the annotationProcessorPaths .mapstruct-processor依赖项添加到您的依赖项中并删除annotationProcessorPaths This way the maven compiler will pick up all the annotation processors that are in your dependencies.这样,maven 编译器将拾取依赖项中的所有注释处理器。

I would advise in using Option 1, as with that you can be certain that you are not using some MapStruct transitive dependencies and internal classes in your code.我建议使用选项 1,因为您可以确定您没有在代码中使用某些MapStruct传递依赖项和内部类。

Edit:编辑:

To make sure that the IntelliJ annotation processing also works you will have to add the mapstruct-processor as a provided dependency due to IDEA-150621 .由于IDEA-150621 ,为了确保 IntelliJ 注释处理也有效,您必须将mapstruct-processor添加为provided的依赖项。 IntelliJ in the moment does not use the annotationProcessorPaths from Maven to configure the project correctly. IntelliJ 目前不使用 Maven 的annotationProcessorPaths来正确配置项目。

Edit 2:编辑2:

Add information and comment about lombok-mapstruct-binding needed from Lombok 1.18.16.添加有关 Lombok 1.18.16 所需lombok-mapstruct-binding信息和评论。

Just in case if somebody is looking for how to configure it using Gradle:以防万一有人正在寻找如何使用 Gradle 进行配置:

dependencies {

   // Lombok
   compileOnly 'org.projectlombok:lombok:1.18.2'
   annotationProcessor 'org.projectlombok:lombok:1.18.2'

   // MapStruct
   compileOnly 'org.mapstruct:mapstruct-jdk8:1.2.0.Final'
   annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'

}

For me solution was realy simple.对我来说,解决方案非常简单。

The order of the processors was important.处理器的顺序很重要。

In my case mapstruct processor was defined before lombok processor.在我的例子中,mapstruct 处理器是在 lombok 处理器之前定义的。 In case with bad order mapstruct does not generate mappings, just create instance of result class (i saw that in generated implementation).如果顺序错误 mapstruct 不生成映射,只需创建结果类的实例(我在生成的实现中看到了)。

My plugin configuration with right order:我的插件配置顺序正确:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven.compiler.plugin.version}</version>
    <configuration>
        <source>15</source>
        <target>15</target>
        <compilerArgs>--enable-preview</compilerArgs>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </path>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${mapstruct.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

And of course you need to add dependencies:当然,您需要添加依赖项:

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
</dependencies>

After multiple attempts in my Spring Boot Maven project, the following finally worked:在我的 Spring Boot Maven 项目中多次尝试后,以下终于奏效了:

Configuration of pom.xml: pom.xml的配置:

  1. The properties section:属性部分:
    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.target>11</maven.compiler.target>
            <maven.compiler.source>11</maven.compiler.source>
            <version.lombok>1.18.18</version.lombok>
            <version.mapstruct>1.4.2.Final</version.mapstruct>
            <version.mapstruct-lombok>0.2.0</version.mapstruct-lombok>
        </properties>
  1. The dependencies section:依赖项部分:
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${version.lombok}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${version.mapstruct}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>${version.mapstruct-lombok}</version>
        </dependency>
  1. The build plugins section:构建插件部分:
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>9</source>
                        <target>9</target>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${version.mapstruct}</version>
                            </path>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${version.lombok}</version>
                            </path>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok-mapstruct-binding</artifactId>
                                <version>${version.mapstruct-lombok}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
            </plugins>
        </build>

Hope this helps, my mapper before did not map the field values, but now these are mapper between the source and the target + the nested list of elements in each is also being mapped along with field values.希望这会有所帮助,我的映射器之前没有映射字段值,但现在这些是源和目标之间的映射器 + 每个元素的嵌套列表也与字段值一起映射。

I had similar issues.我有类似的问题。 Turned out my MapStruct version was outdated!原来我的 MapStruct 版本已经过时了!

I used MapStruct version 1.1.0.Final, but for Lombok support at least 1.2.0.Final is required .我使用 MapStruct 版本 1.1.0.Final,但对于 Lombok 支持,至少需要 1.2.0.Final

If anyone is reading this when using the latest Lombok and Mapstruct library versions :-如果有人在使用最新的 Lombok 和 Mapstruct 库版本时阅读此内容:-

If you are using Lombok 1.18.16 or newer then you also need to add lombok-mapstruct-binding in order to make Lombok and MapStruct work together.如果您使用的是 Lombok 1.18.16 或更高版本,则还需要添加lombok-mapstruct-binding以使 Lombok 和 MapStruct 一起工作。

https://mapstruct.org/faq/#Can-I-use-MapStruct-together-with-Project-Lombok https://mapstruct.org/faq/#Can-I-use-MapStruct-together-with-Project-Lombok

For issues after upgrading Intellij to 2020.3 version, following solution worked for me.对于将 Intellij 升级到 2020.3 版本后的问题,以下解决方案对我有用。 Use lombok version: 1.18.16, lombok-mapstruct-binding: 0.2.0使用 lombok 版本:1.18.16,lombok-mapstruct-binding:0.2.0

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
</dependency>
<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
</dependency>

Change the order of the lombok and mapstruct plugin path in annotation processing section of plugins:在插件的注释处理部分更改lombok和mapstruct插件路径的顺序:

<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>
                <version>3.5.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${org.projectlombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>

                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>

Apparently this is how you are supposed to load multiple annotation processors in Gradle显然这就是你应该如何在 Gradle 中加载多个注释处理器

dependencies {
    implementation "org.mapstruct:mapstruct:1.4.2.Final", "org.projectlombok:lombok:1.18.20"
    annotationProcessor "org.mapstruct:mapstruct-processor:1.4.2.Final", "org.projectlombok:lombok:1.18.20", "org.projectlombok:lombok-mapstruct-binding:0.2.0"
}

If someone is still looking for this answer, I faced a similar issue with JOOQ + MapStruct Integration.如果有人仍在寻找这个答案,我在 JOOQ + MapStruct Integration 中遇到了类似的问题。 The answer is the order of paths matters答案是路径的顺序很重要

  • IntellijIdea 2020.2.3 IntellijIdea 2020.2.3
  • Java 8爪哇 8
  • Spring Framework 2.4.0 Spring 框架 2.4.0

My build snippet我的构建片段

<build>
    <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>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${org.lombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                    <!-- This is needed when using Lombok 1.8.16 and above -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>${org.lombok-mapstruct-binding.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
        </plugin>
    </plugins>
</build>

The mapstruct was just returning a null instance. mapstruct 只是返回一个空实例。 Recently I upgraded the Intellij version to 2020.3, and spring boot 2.4.0, and landed into this situation.最近我将Intellij版本升级到2020.3,spring boot 2.4.0,落到了这种情况。

When I changed the spring version to 2.3.o.RELEASE, I could see after compilation is that mapstruct was building the mappings properly.当我将 spring 版本更改为 2.3.o.RELEASE 时,编译后我可以看到 mapstruct 正在正确构建映射。

Going through the post that mapstruct has recently updated.. but it will take time.. :) so, for now, I moved from maven to Gradle project and all working absolutely fine.. sounds weird but true...浏览 mapstruct 最近更新的帖子.. 但这需要时间.. :) 所以,就目前而言,我从 maven 转移到 Gradle 项目并且一切工作都很好.. 听起来很奇怪但确实如此......

I would be thankful if anyone would post what exactly the right issue and what's the solution.如果有人能发布正确的问题和解决方案,我将不胜感激。

I agree that the order does play its role, also when using gradle.我同意订单确实发挥了作用,在使用 gradle 时也是如此。 In my case it was failing as soon I was adding subprojects { ... dependencyManagement{}} to the parent build.gradle in a multimodule setup.在我的情况下,当我在多模块设置中将subprojects { ... dependencyManagement{}}添加到父build.gradle时,它​​就失败了。

I feel a right way to declare versions in pom.xml is to refer to something like this for versions instead of manually specifying versions of everything.我觉得在 pom.xml 中声明版本的正确方法是为版本引用类似内容,而不是手动指定所有内容的版本。 eg maven-compiler-plugin.version , lombok.version is already present there例如maven-compiler-plugin.version , lombok.version已经存在

properties block -属性块 -

<properties>
  <java.version>11</java.version>
  <mapstruct.version>1.4.2.Final</mapstruct.version>
  <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>

dependencies block -依赖块 -

<dependencies>
  <!-- ... -->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>

  <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct</artifactId>
      <version>${mapstruct.version}</version>
  </dependency>

  <dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${mapstruct.version}</version>
  </dependency>

  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-mapstruct-binding</artifactId>
    <version>${lombok-mapstruct-binding.version}</version>
  </dependency>

  <!-- ... -->
</dependencies>

build block -构建块 -

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
          </exclude>
        </excludes>
      </configuration>
    </plugin>

    <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>
        <annotationProcessorPaths>
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
          </path>

          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>${lombok-mapstruct-binding.version}</version>
          </path>

          <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
          </path>
        </annotationProcessorPaths>
      </configuration>
    </plugin>
  </plugins>
</build>

For Spring-Boot the simplest configuration with exclude processing code from result jar:对于 Spring-Boot 最简单的配置,从结果 jar 中排除处理代码:

pom.xml

     <dependencies>
         <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        ....
    </dependencies>
    <build>
        <plugins>
            <!-- exclude from jar-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>

Without lombok-mapstruct-binding it also works fine.没有lombok-mapstruct-binding它也可以正常工作。
Maybe somebody comments, what for does it needed?也许有人评论,它需要什么?

In addition, also, correct versions have to be used.此外,还必须使用正确的版本。

See here这里

For people who uses build gradle, this is the .gradle file I successfully created对于使用 build gradle 的人来说,这是我成功创建的 .gradle 文件

dependencies {

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation "org.projectlombok:lombok-mapstruct-binding:${gradleMapstructVersion}"

implementation "org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"

}

The things is that, as said above, we have to use the org.projectlombok:lombok-mapstruct-binding in order to use mapstruct with lombok事情是,如上所述,我们必须使用org.projectlombok:lombok-mapstruct-binding才能将 mapstruct 与 lombok 一起使用

It is enough to add the following dependencies:添加以下依赖项就足够了:

implementation("org.mapstruct:mapstruct:1.4.2.Final")
annotationProcessor("org.mapstruct:mapstruct-processor:1.4.2.Final")

compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok-mapstruct-binding:0.2.0")

Format: Gradle(Kotlin)格式:摇篮(科特林)

If you have reached till here, that means none of the above solutions worked for you.如果您已经到达这里,则意味着上述解决方案都不适合您。 Finally what worked for me was:最后对我有用的是:

  1. The following order of annotation processor in pom.xml pom.xml中注解处理器的顺序如下
<annotationProcessorPaths>
 ...
       <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </path>
    <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </path>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-mapstruct-binding</artifactId>
        <version>0.2.0</version>
    </path>
 ...
</annotationProcessorPaths>
  1. The following setting of Annotation Processors in Intellij IDEA Preferences. Intellij IDEA 首选项中注释处理器的以下设置。 Make sure your project is in the right profile.确保您的项目处于正确的配置文件中。 In my case the profile is Annotation Profile for Alippo and project is alippo .在我的例子中,配置文件是Annotation Profile for Alippo ,项目是alippo I really don't know how the configuration made the difference.我真的不知道配置是如何产生差异的。

在此处输入图像描述

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

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