简体   繁体   English

MapStruct 不生成实现类

[英]MapStruct is not generating implementation classes

I am using Mapstruct and Lombok with maven and IDEA and it is refusing to generate mappers implementation.我在 maven 和 IDEA 中使用 Mapstruct 和 Lombok,它拒绝生成映射器实现。 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: Mapstruct 和 Lombok 都注册为具有想法的注释处理器: 在此处输入图片说明

Implementation files does not generate when I try to build with IDEA or if I try maven clean install.当我尝试使用 IDEA 构建或尝试 maven 全新安装时,不会生成实现文件。

I have tried changing Java from 11 to 8 and it still does not work.我尝试将 Java 从 11 更改为 8,但它仍然不起作用。 /target/generated-sources/annotations is just empty. /target/generated-sources/annotations 只是空的。 Other project with same config is wroking fine.其他具有相同配置的项目运行良好。

问题是我的映射器缺少注释@Mapper

添加mapstruct-processor依赖项为我解决了这个问题。

If you use kotlin, you need to use kapt instead of annotationProcessor如果使用 kotlin,则需要使用kapt而不是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之后如果你执行gradle build就会生成实现

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 Java:由于错误元素存在问题,没有为 Mapper 创建实现

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当我将版本降低到2.2.6.RELEASE它开始工作时,我曾经使用 spring 启动版本2.4.3

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>

If you are using Kotlin with maven, you can add these dependencies:如果您在 maven 中使用 Kotlin,则可以添加以下依赖项:

    <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:并将 kapt 执行添加到 kotlin-maven-plugin 中,如下所示:

   <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM