Home >Java >javaTutorial >Why Does My Project Fail to Compile When Using MapStruct and Lombok Together?

Why Does My Project Fail to Compile When Using MapStruct and Lombok Together?

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 09:09:18798browse

Why Does My Project Fail to Compile When Using MapStruct and Lombok Together?

MapStruct and Lombok Integration with Project Not Compiling

Issue:

When using MapStruct and Lombok together in a project, errors arise due to the unknown property "id" in the result type when getters and setters are removed with @Getter and @Setter annotations.

Resolution:

The issue stems from Maven using only the MapStruct processor but not the Lombok processor. To address this, two options are available:

Option 1: Add Lombok Dependency to Annotation Processor Paths

Modify the maven-compiler-plugin configuration in pom.xml to include the Lombok dependency in the annotationProcessorPaths:

<!-- Maven Compiler Configuration -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <!-- ... Existing Configuration Snippet -->

        <annotationProcessorPaths>
            <!-- Lombok Dependency -->
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${org.projectlombok.version}</version>
            </path>

            <!-- MapStruct Dependency -->
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

Option 2: Add MapStruct-Processor Dependency

Alternatively, add the mapstruct-processor dependency to the dependencies in pom.xml and remove the annotationProcessorPaths:

<!-- Maven Dependencies -->
<dependencies>
    <!-- ... Existing Dependencies Snippet -->

    <!-- MapStruct Processor Dependency -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
</dependencies>

Additional Notes:

  • For Lombok 1.18.16 and above, include the lombok-mapstruct-binding dependency in the annotationProcessorPaths as shown in Option 1.
  • Ensure that the MapStruct processor is marked as "provided" in the pom.xml dependencies to enable annotation processing in IntelliJ.
  • If this issue persists after implementing the solutions above, validate if your Java and MapStruct versions are compatible. Refer to the latest MapStruct documentation for compatibility information.

The above is the detailed content of Why Does My Project Fail to Compile When Using MapStruct and Lombok Together?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn