The “Cannot find symbol” error when using MapStruct with Lombok

MapStruct Logo

If you are having the problem “Cannot find symbol” with your Java project compilation because the compiler doesn’t recognize the methods calls of getters, setters, constructors, etc., but your IDE is still recognizing the methods normally, you probably forgot to configure your pom.xml or build.gradle for multiple annotation processors with the MapStruct library.

image
WARNING: If you’re using Intellij IDE check if the option “Enable annotation processing” is checked.

If your problem is more general, you can try the solutions provided in the Baeldung post.

In my case, the problem of “Cannot find symbol” was that I forgot to pass all the paths of the annotation processors that I was using to the Maven compiler. In the examples of MapStruct project, you can check the correct configurations to make it work together with Lombok correctly.

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.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>
            <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>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>${lombok-mapstruct-binding}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
...

Good to remember:

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.

https://mapstruct.org/faq/
  1. https://github.com/mapstruct/mapstruct-examples/
  2. https://mapstruct.org/documentation/installation/

Leave a Reply

Your email address will not be published. Required fields are marked *