MapStruct and Lombok not working together
The reason why it does not work is because Maven only uses the MapStruct processor and not the Lombok one. The annotationProcessorPaths
tells maven which processors it should use.
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.
You have 2 options:
Option 1: Add the lombok dependency in the annotationProcessorPaths
<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:
Add the mapstruct-processor
dependency to your dependencies and remove the annotationProcessorPaths
. This way the maven compiler will pick up all the annotation processors that are in your dependencies.
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.
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. IntelliJ in the moment does not use the annotationProcessorPaths
from Maven to configure the project correctly.
Edit 2:
Add information and comment about lombok-mapstruct-binding
needed from Lombok 1.18.16.
SpringBoot + Lombok + MapStruct not working together
Just double checked everything, I was missing some intelliJ configurations.
Mapstruct and Lombok unexpected result with Spring Boot 2.4 and above Is there any workaround or issues with this version?
Your problem is that because of this in your pom.xml
:
<version>${lombok.version}</version>
you are getting the Lombok version from the parent pom of your parent pom, spring-boot-starter-parent
(your grandparent pom?), and thus with Spring Boot 2.4.0 you are picking up a newer version of Lombok.
Starting with Lombok 1.18.16, you need to add another dependency. See this answer in the MapStruct FAQ:
If you are using Lombok 1.18.16 or newer you also need to add lombok-mapstruct-binding in order to make Lombok and MapStruct work together.
They have a sample project to demonstrate how to use them together. Here are the relevant parts of the sample pom.xml
:
<properties>
<!-- . . . -->
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
<!-- . . . -->
<annotationProcessorPaths>
<!-- . . . -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
How to instruct Mapstruct to use lombok builder?
I've played around with your demo sources, and it turns out that placing Mapstruct first in the compiler plugin sources fixes the issue. I don't know why this works, it feels counterintuitive to me, but this is what one of the developers of Mapstruct suggested in a similar issue posted on Lombok's Github.
So in your case:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.mapstruct-processor.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${org.projectlombok.ombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
spring-boot 2.5.2 with mapstruct\lombok and swagger2 (mapstruct not work)
my bad, i did't notice the
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
change to
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
is worked.
Related Topics
What Is the Use of System.In.Read()
How to Capitalize the First Letter of a String in Java
How Apply CSS on a <H:Inputtext>
How to Disable Sslv3 in Android for Httpsurlconnection
String.Equals() with Multiple Conditions (And One Action on Result)
How to Catch a Firebase Auth Specific Exceptions
Detecting State Changes Made to the Bluetoothadapter
Playing Bg Music Across Activities in Android
Deserializing Generic Types with Gson
Alertdialog from Within Broadcastreceiver? Can It Be Done
Preventing System.Exit() from API
Find First Element by Predicate
Unobtrusive Way to Combine and Compress JavaScript/CSS for Java/Spring/Maven Applications
Java.Lang.Runtimeexception: Takepicture Failed