MapStruct + Lombok together not compiling: unknown property in result type
Asked Answered
N

23

188

Tech Stack being used :

Java 8 MapStruct : 1.2.0.Final Lombok: 1.16.18 IDE: IntelliJ - Lombok Plugin already installed

  • 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"?
  • 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.
  • One more solution that I have already tried is running Lombok's Delombok plugin, but still, the same issue is arising.

Below are the project files :

The Entity 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 :

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

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

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

Nerta answered 6/12, 2017 at 14:14 Comment(0)
C
307

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.

Champaigne answered 6/12, 2017 at 22:16 Comment(17)
I think "lombok" should come first then "mapstruct-processor"Jerrilyn
The order should not make a difference. We were explicitly working on that with the Lombok teamChampaigne
I think this information definitively needs to be added to mapstructs FAQ. Their example project is working without annotation processors defined in mavens build section, but I think they could mention it there (took me some time to find the solution here).Cathicathie
Here is a full example from MapStructs GitHub (Gradle and Maven): github.com/mapstruct/mapstruct-examples/blob/master/…Bontebok
Confirmed the same behavior at IntelliJ 2020.2.3 a this time (dont forget "provided" in dependency)Ryter
Thank you for the comment about lombok-mapstruct-binding (mapstruct.org/faq/…). Available version: 0.2.0. Only one thing <!-- This is needed when using Lombok 1.8.16 and above --> => <!-- This is needed when using Lombok 1.18.16 and above -->Duwe
lombok-mapstruct-binding solved my issues. Thanks a lot!Plutocrat
Option 1 solved this for me, but only when I placed the mapstruct path before lombok.Gravois
adding Lombok before Mapstruct processor worked for me aswell @DonSrinathSeiden
It seems the order is important. Adding Lombok before Mapstruct processor worked for me too.Reiners
Option 2 does not work for me with Lombok 1.18.18Nobell
For people who have tried changing the order of dependencies but it was not resolved, please try changing the version of lombokTownes
For us, annotation processor path order mattered, otherwise mapstruct ignored the builder and tried to use a non-accessible all-args-constructor. For mapstruct 1.4.2.Final and lombok 1.18.20 we had to use mapstruct-processor at first. the order of lombok and lombok-mapstruct-binding did not matter.Cathicathie
For option 1 when using gradle, use annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0" to resolve the issue.Fellner
the mapstruct (with lombok) documentation : mapstruct.org/documentation/stable/reference/html/#_set_upCantankerous
@DonSrinath yes, order matters. Tested versions of lombok and mapstruct are 1.18.22 and 1.3.0.Final, respectivelyHibernia
When declaring lombok first and then mapstruct, it's not possible to use @SuperBuilder #68607842Singlecross
P
41

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'

}
Platinic answered 19/10, 2018 at 1:20 Comment(3)
This will no longer work without lombok-mapstruct-bindingThermolysis
@PiotrŻak how is that?Servo
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. mapstruct.org/faqThermolysis
T
29

After multiple attempts in my Spring Boot Maven project, the following finally worked:

Configuration of 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.

Torietorii answered 25/3, 2021 at 6:33 Comment(1)
Still actual in dec.2021, thank youRude
P
23

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>
Platinic answered 26/11, 2020 at 12:13 Comment(2)
Hi, I got those paths in that order but It didn't help.Mirtamirth
@denu, agree, I don't think the order matters, not a helpful answerPerron
O
7

I had similar issues. Turned out my MapStruct version was outdated!

I used MapStruct version 1.1.0.Final, but for Lombok support at least 1.2.0.Final is required.

Origami answered 23/1, 2019 at 14:56 Comment(0)
C
6

If anyone is reading this when using the latest Lombok and Mapstruct library versions:-

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.

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

Cantor answered 10/7, 2021 at 15:29 Comment(0)
N
6

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. e.g. maven-compiler-plugin.version, lombok.version is already present there

properties block -

<properties>
  <java.version>17</java.version>
  <mapstruct.version>1.5.5.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>
</dependencies>

build block -

<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>${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>
Nero answered 19/2, 2022 at 3:8 Comment(1)
Thank you. For me, it worked using just : <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path>Symposium
A
4

For issues after upgrading Intellij to 2020.3 version, following solution worked for me. Use lombok version: 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:

<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>
Aruspex answered 29/12, 2020 at 6:12 Comment(0)
C
3

Apparently this is how you are supposed to load multiple annotation processors in 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"
}
Cooperage answered 7/9, 2021 at 18:48 Comment(0)
C
2

If someone is still looking for this answer, I faced a similar issue with JOOQ + MapStruct Integration. The answer is the order of paths matters

  • IntellijIdea 2020.2.3
  • Java 8
  • Spring Framework 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>
Crystallization answered 19/11, 2020 at 11:47 Comment(0)
H
2

The mapstruct was just returning a null instance. Recently I upgraded the Intellij version to 2020.3, and spring boot 2.4.0, and landed into this situation.

When I changed the spring version to 2.3.o.RELEASE, I could see after compilation is that mapstruct was building the mappings properly.

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

I would be thankful if anyone would post what exactly the right issue and what's the solution.

Hurlow answered 10/12, 2020 at 18:24 Comment(0)
F
2

For people who uses build gradle, this is the .gradle file I successfully created

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

Formenti answered 22/5, 2022 at 3:45 Comment(0)
B
2

If you're using a maven root module and submodules with dependency management, then you need to ensure that the order in your submodule configuration corresponds to the items that you've put in the root pom:

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

Ensure the same order in the submodule!:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Not a good user experience for a library that is widely promoted, fix this for the sake of the Java community!

Berner answered 23/4, 2023 at 22:37 Comment(0)
T
2

No need for lombok-mapstruct-binding

Just put lombok before mapstruct-processor

ext {
    lombokVersion = '1.18.28'
    mapstructVersion = '1.5.5.Final'
}

dependencies {
    // Lombok - Important : Lombok before mapstruct
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"

    // Mapstruct
    compileOnly "org.mapstruct:mapstruct:${mapstructVersion}"
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
}
Tobietobin answered 4/8, 2023 at 13:17 Comment(0)
F
1

I agree that the order does play its role, also when using gradle. In my case it was failing as soon I was adding subprojects { ... dependencyManagement{}} to the parent build.gradle in a multimodule setup.

Figurate answered 3/12, 2020 at 8:52 Comment(0)
M
1

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
<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. Make sure your project is in the right profile. In my case the profile is Annotation Profile for Alippo and project is alippo. I really don't know how the configuration made the difference.

enter image description here

Mussolini answered 31/1, 2023 at 14:13 Comment(0)
L
1

In my case, switching the order of the dependencies inside build.gradle so that lombok is first and mapstruct second was the only way to make it work.

I wasn't even aware that the order mattered in the dependencies list, but I've done nothing else and it's working perfectly now.

Throws compilation error because it cannot find any properties at all in the java classes:

    implementation 'org.mapstruct:mapstruct:1.5.3.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

Works perfectly:

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    implementation 'org.mapstruct:mapstruct:1.5.3.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'
Lezley answered 4/8, 2023 at 12:46 Comment(0)
E
0

For Spring-Boot the simplest configuration with exclude processing code from result 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.
Maybe somebody comments, what for does it needed?

Eurus answered 14/8, 2021 at 20:58 Comment(0)
D
0

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)

Donohoe answered 30/12, 2022 at 4:48 Comment(0)
S
0

Use this

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <dependency>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </dependency>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>
                        -Amapstruct.defaultComponentModel=spring
                    </compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
Starlight answered 13/3, 2023 at 6:47 Comment(0)
D
0

If you are using Gradle Kotlin dsl, try this in your build.gradle.kts file:

plugins {
   // ...
   val kotlinVersion = "1.9.10"

   kotlin("jvm") version kotlinVersion
   kotlin("kapt") version kotlinVersion
   // ...
}
// ...
dependencies {
  // ...
  kapt("org.mapstruct:mapstruct-processor")
  kapt("org.springframework.boot:spring-boot-configuration-processor")
  implementation("org.mapstruct:mapstruct:1.5.5.Final")
  compileOnly("org.projectlombok:lombok:1.18.16")
  annotationProcessor("org.projectlombok:lombok:1.18.16")
  annotationProcessor("org.projectlombok:lombok-mapstruct-binding:0.2.0")
  // ...
}
// ...
kapt {
  keepJavacAnnotationProcessors = true
  // if needed, here is an example of mapStruct global config
  arguments {
    arg("mapstruct.defaultComponentModel", "spring")
  }
}

You would need to be sure that annotation processors has been enabled in your IDE and lombok plugins has been also installed

Difficult answered 1/11, 2023 at 15:45 Comment(0)
C
0

So for recap you must get ride of those points:

  1. Check the preprocessors orders, in some cases chenging the order , try to putting the mapstruct path to the last place.
  2. Check if with mvn compile properly, in that case is an intellij IDE problem
  3. Try to specify in the POM the latest version of Lombok and so on until you find a working version in intellij
Corallite answered 21/12, 2023 at 11:45 Comment(0)
S
-1

i have working mapstruct + lombok with following versions

  1. java 17
  2. mapstruct 1.3.1.Final
  3. spring boot 2.7.6
  4. lombok 1.18.26
  5. lombok-mapstruct-binding 0.2.0
  6. maven-compiler-plugin 3.8.1

Map struct > 1.4 versions dosent work with latest lombok versions. So in updated versions i have to downgrade mapstruct to make it work

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.3.1.Final</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>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.26</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
Supervise answered 27/2, 2023 at 14:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.