13

I am using Mapstruct and Lombok with maven and IDEA and it is refusing to generate mappers implementation. Config:

<?xml version="1.0" encoding="UTF-8"?>
<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>
...

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <java.version>11</java.version>
        <org.mapstruct.version>1.3.0.Beta2</org.mapstruct.version>
        <lombok.version>1.18.2</lombok.version>
    </properties>

    <dependencies>
        <!-- spring deps -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        ...

        <!-- lombok dep -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources/java/</source>
                                <source>${project.build.directory}/generated-sources/annotations/</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Implementation-Version>${project.version}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

   ...
</project>

Both Mapstruct and Lombok are registered as annotations processors with idea: enter image description here

Implementation files does not generate when I try to build with IDEA or if I try maven clean install.

I have tried changing Java from 11 to 8 and it still does not work. /target/generated-sources/annotations is just empty. Other project with same config is wroking fine.

user3495816
  • 567
  • 1
  • 6
  • 17
  • Does it work when you run it only with maven? Which version of IntelliJ are you using? – Filip Jan 31 '19 at 18:44
  • could it be that `build-helper-maven-plugin` conflicts with generating resources? Could you comment that one out and see what happens? – Sjaak Jan 31 '19 at 19:13

7 Answers7

7

Adding mapstruct-processor dependency solved the issue for me.

Ankit Chauhan
  • 646
  • 6
  • 20
  • Adding the gradle dependency which worked for me. implementation 'org.mapstruct:mapstruct-processor:1.0.0.Beta3' Note - Its terrible that I can’t format comment in stackoverflow – Akash Verma Aug 16 '22 at 08:19
6

If you use kotlin, you need to use kapt instead of annotationProcessor

Gradle example:

plugins {
  kotlin("kapt") version "1.4.32"
}
...
dependencies {
  ...
  implementation("org.mapstruct:mapstruct:1.4.2.Final")
  kapt("org.mapstruct:mapstruct-processor:1.4.2.Final")
}

Afterwards the implementation is generated if you execute gradle build

anstue
  • 990
  • 12
  • 24
  • adding kotlin("kapt") version "1.4.32" in plugins {} block results in error saying "only id(String) method calls allowed in plugins {} script block" – Akash Verma Aug 11 '22 at 21:07
4

Problem was that my mappers were missing annotation @Mapper.

user3495816
  • 567
  • 1
  • 6
  • 17
1

For me the problem was:

<useIncrementalCompilation>false</useIncrementalCompilation>

After commented that out, all works!

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>${jdk.target.version}</source>
        <target>${jdk.target.version}</target>
        <!--<useIncrementalCompilation>false</useIncrementalCompilation>-->
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
  • Mattia, this does not look like an answer for OP question. As you can see `useIncrementalCompilation` was not used. – J.Wincewicz Oct 21 '21 at 13:25
  • You are right! I just wanted to share my solution in case someone has the same kind of problem as me. I think it might be useful for some people. – Mattia Casella Oct 22 '21 at 05:50
  • @MattiaCasella I'm glad you left this here. It is a solution to the problem why Mapstruct was not generating the implementation class for the Mapper. The solution as shown in your answer was to add it to the annotation processor path – SamwellTarly Aug 22 '22 at 14:20
1

If you are using Kotlin with maven, you can add these dependencies:

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.4.2.Final</version>
    </dependency>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.4.2.Final</version>
    </dependency>

And add the kapt execution to the kotlin-maven-plugin as follows:

   <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <id>kapt</id>
                        <goals>
                            <goal>kapt</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>src/main/kotlin</sourceDir>
                                <sourceDir>src/main/java</sourceDir>
                            </sourceDirs>
                            <annotationProcessorPaths>
                                <!-- Specify your annotation processors here. -->
                                <annotationProcessorPath>
                                    <groupId>com.google.dagger</groupId>
                                    <artifactId>dagger-compiler</artifactId>
                                    <version>2.9</version>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
DeveloperKurt
  • 758
  • 1
  • 7
  • 21
0

I have come across this issue. In my case gets this error:

Java: No implementation was created for Mapper due to having a problem in the erroneous element

when the app is running.

I used to spring boot version 2.4.3 when I decrease version to 2.2.6.RELEASE it start to work

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ulug'bek
  • 2,762
  • 6
  • 31
  • 59
0

Try Properties > Maven > Annotation Processing > (Enable) Automatically configure JDT APT.

DH12
  • 1