MapStruct and Lombok not working together MapStruct and Lombok not working together java java

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.


Just in case if somebody is looking for how to configure it using 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. In case with bad order mapstruct does not generate mappings, just create instance of result class (i saw that in generated implementation).

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>