MapStruct and Lombok not working together

前端 未结 5 880
陌清茗
陌清茗 2020-11-28 05:05

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
相关标签:
5条回答
  • 2020-11-28 05:34

    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'
    
    }
    
    0 讨论(0)
  • 2020-11-28 05:42

    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.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>${org.mapstruct.version}</version>
                </path>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>${org.projectlombok.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>0.1.0</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 about lombok-mapstruct-binding needed from Lombok 1.18.16.

    0 讨论(0)
  • 2020-11-28 05:54

    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.

    0 讨论(0)
  • 2020-11-28 05:54

    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>
    
    0 讨论(0)
  • 2020-11-28 05:56

    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>
    
    0 讨论(0)
提交回复
热议问题