Explain Codes LogoExplain Codes Logo

Mapstruct + Lombok together not compiling: unknown property in result type

java
lombok
mapstruct
annotation-processing
Anton ShumikhinbyAnton Shumikhin·Feb 28, 2025
⚡TLDR

Let's get Lombok and MapStruct in line to avoid those pesky compilation errors. Your mission, if you choose to accept it, is to give your maven-compiler-plugin The Order:

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <annotationProcessorPaths> <!-- Lombok first, because it can't wait --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>LATEST_LOMBOK_VERSION</version> </path> <!-- Then MapStruct, because it's patient --> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>LATEST_MAPSTRUCT_VERSION</version> </path> </annotationProcessorPaths> <!-- Because we care about compatibility --> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>

Don't forget to replace LATEST_LOMBOK_VERSION and LATEST_MAPSTRUCT_VERSION with your current versions. This strategy ensures Lombok's eager code generation finishes before MapStruct—и voilà, no more unknown property errors.

Harmonizing Lombok and MapStruct: Configurations and setups

Maven and Gradle: The dynamic duo

With Maven, you should add the lombok-mapstruct-binding; it's the peace treaty for Lombok version 1.18.16 and above. Who knew it was that easy to solve interoperability issues?

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>0.2.0</version> <scope>provided</scope> <!-- Because we're generous --> </dependency>

For those of you who are on team Gradle, here's your playbook:

annotationProcessor 'org.projectlombok:lombok:latest' // First in, first out. compileOnly 'org.projectlombok:lombok:latest' // Keeps it light. annotationProcessor 'org.mapstruct:mapstruct-processor:latest' // Patience is a virtue.

Again, replace latest with the latest versions. We don't want you living in the past.

IntelliJ: The master of ceremony

Even IntelliJ has a role to play. Enable its annotation processing by:

  • Exploring Settings / Preferences.
  • Treading the path to Build, Execution, Deployment > Compiler > Annotation Processors.
  • Embracing the Enable annotation processing.

You might have to make sure the source and target are set to Java 8 or your respective version in IntelliJ's settings. An educated IDE equals a smooth compilation process.

Pragmatic guide: Unlocking a seamless integration

Conflict resolution

You may get your hands dirty when smoothing out:

  • Delombok. Don't create pandemonium by stripping out Lombok annotations before MapStruct can see them.
  • Rely on @Getter and @Setter when necessary instead of @Data. It minimums magic but puts you on the driver's seat.
  • Keep @MapperConfig at bay with MapStruct when it cross paths with Lombok.

Under the hood

To play Sherlock, inspect the generated implementations post a clean build:

  • Check if all Lombok generated methods are in the brigade.
  • Ensure MapStruct's masterpieces have the desired mappings.

Maven setting

Set your project's source and encoding to UTF-8 to ward off any "lost in translation" issues.

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- because UTF-8 is cufflinks, others are sleeve buttons--> </properties>

Keep libraries fancy and fresh

Consistent updates of both Lombok and MapStruct to compatible versions keep things running like a Swiss watch.