首页 > 其他分享 >MapStruct与lombok加载顺序问题与annotationProcessorPaths的关系?

MapStruct与lombok加载顺序问题与annotationProcessorPaths的关系?

时间:2022-12-04 22:01:00浏览次数:57  
标签:MapStruct annotationProcessorPaths processor mapstruct org lombok Lombok

MapStruct是什么?

MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.——https://mapstruct.org/
从官方定义来看,MapStruct类似于我们熟悉的BeanUtils, 是一个Bean的转换框架。
In contrast to other mapping frameworks MapStruct generates bean mappings at compile-time which ensures a high performance, allows for fast developer feedback and thorough error checking.——https://mapstruct.org/
他与BeanUtils最大的不同之处在于,其并不是在程序运行过程中通过反射进行字段复制的,而是在编译期生成用于字段复制的代码(类似于Lombok生成get()和set()方法),这种特性使得该框架在运行时相比于BeanUtils有很大的性能提升。

lombok

这个大家都很熟悉,生成getset方法,那么mapstuct要依赖这种方法。

MapStuct与lombok的引入的正确关系

由于MapStruct和Lombok都会在编译期生成代码,如果配置不当,则会产生冲突,因此在工程中同时使用这两个包时,应该按照以下方案导入:

当POM中不包含Lombok时

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

当POM中包含Lombok且不包含

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>

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

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

注意:引入时,mapstruct-processor必须lombok后面。

当POM中包含Lombok且包含

<properties>
    <org.mapstruct.version>1.5.2.Final</org.mapstruct.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</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>1.18.12</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

问题产生

如果lombok在mapstuct后面,则会产生问题

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.0.Final</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.5.0.Final</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

mapstruct编译原理

为了探究上述问题产生的原因,我们首先要理解MapStruct的基本原理。

MapStruct与其他Bean映射库最大的不同就是,其在编译期间生成转换代码,而不是在运行时通过反射生成代码。

为了更直观的理解这一点,可以从target中找到MapStruct自动生成的对应的ConveterImpl类

即MapStruct为我们编写的Convert抽象类自动生成了一个实现。

而Lombok也是在编译时自动生成代码,那么问题大概率就出现在这里了。

MapStruct是如何与Lombok共存的?

查阅MapStruct官方文档可以发现这样一段内容:

其中提到,MapStruct的annotation processor必须在Lombok的annotation processor生成完代码之后,才可以正常运行。

所以,这应该就是在导入dependencies时,必须先导入Lombok包,再导入MapStruct-processor包才可以正常运行的原因了。不过还有个问题没有解决:

Maven到底在哪里规定了annotation processor的载入顺序?难道每次创建工程时,必须记住这些包导入顺序么?

MapStruct官方推荐的导入流程

在进一步查看MapStruct官网时发现,其并没有将MapStruct-processor放在dependencies中,而是放在了annotationProcessorPaths层级下:

https://mapstruct.org/documentation/installation/

...

<org.mapstruct.version>1.5.2.Final</org.mapstruct.version>

...


org.mapstruct
mapstruct
${org.mapstruct.version}


...



org.apache.maven.plugins
maven-compiler-plugin
3.8.1

1.8
1.8

                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>${org.mapstruct.version}</version>
                </path>
                <!-- other annotation processors -->
            </annotationProcessorPaths>
        </configuration>
    </plugin>
</plugins>
这又是为什么呢?

查阅Maven官方文档,对于有这样一段描述:

If specified, the compiler will detect annotation processors only in those classpath elements. If omitted, the default classpath is used to detect annotation processors. The detection itself depends on the configuration of annotationProcessors.

即如果有层级,则使用这个层级声明注解处理器的顺序执行,如果没有,则按照默认classpath的顺序来使用注解处理器。

地址:https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#annotationProcessorPaths

地址:https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#annotationProcessorPaths

我们接下来以下命令来获取当前Maven项目中的classpath:

mvn dependency:build-classpath -Dmdep.outputFile=classPath.txt
从导出内容可以看出,classPath中的Jar包顺序就是与dependencies中导入的顺序是相同的。

自此,关于MapStruct导入顺序的所有问题均已经被解决,总结如下:

在POM中没有annotationProcessorPaths时,Maven使用的classPath作为注解处理器执行的顺序,而classPath的顺序正是dependencies中导入的顺序。
当MapStruct依赖在Lombok依赖前面时,在执行注解处理器期间, 由于Lombok还未生成get、set代码,因此在MapStruct看来,这些类并没有公开的成员变量,也就无从生成用于转换的方法。
在使用annotationProcessorPaths后,其强制规定了注解处理器的顺序,dependencies中的顺序就被忽略了,Maven一定会先运行Lombok再运行MapStruct,代码即可正常运行。

标签:MapStruct,annotationProcessorPaths,processor,mapstruct,org,lombok,Lombok
From: https://www.cnblogs.com/jichi/p/16950932.html

相关文章

  • 关于Lombok不常见的使用方式
    packagecom.example.cisum.utils;importlombok.*;importlombok.experimental.Accessors;importlombok.experimental.FieldNameConstants;importlombok.extern.......
  • 使用MapStruct 解决对象之间转换、深拷贝问题
    在日常开发中,我们会定义多种不同的Javabean,比如DTO(DataTransferObject:数据传输对象),DO(DataObject:数据库映射对象,与数据库一一映射),VO(ViewObject:显示层对象,通常是Web向模......
  • 记录一个异常 Gradle打包项目Lombok不生效 No serializer found for class com.qbb.Us
    完整的错误:03-Dec-202216:57:22.941涓ラ噸[http-nio-8080-exec-5]org.apache.catalina.core.StandardWrapperValve.invoke鍦ㄨ矾寰勪负/ssm鐨勪笂涓嬫枃涓紝Servle......
  • 探究lombok-02-类继承
    Java8org.springframework.boot2.7.3lombok1.18.24EclipseVersion:2022-09(4.25.0)--ben发布于博客园 上一篇:探究lombok-01https://www.cnblogs.com/luo630/......
  • eclipse使用lombok @Data注解仍然提示没有get/set方法
    在换了新电脑之后,安装好eclipse之后,测试了一下lombok插件,随便写了一个实体类之后,发现提示未使用。这时我就感觉应该是set/get方法没有正常生成的问题。在另一个类里调用App......
  • lombok 理解
    @SneakyThrows注解有的jar包,没有源码看到的class文件时这样的publicBladeFileputFile(MultipartFilefile){try{returnthis.putFile(this.......
  • 探究lombok-01
    Java8org.springframework.boot2.7.3lombok1.18.24EclipseVersion:2022-09(4.25.0)-- 0、序ProjectLombok:https://projectlombok.org/ 经常用lombok,特......
  • 8_Lombok
    使用步骤:在IDEA中安装Lombok插件在项目中导入lombok的jar包<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><vers......
  • Lombok库和实体类表单校验--Bean Validation API
    它能够在运行时动态生成getter,setter方法,以及equals()、hashCode()、toString()这些方法;类级别的@Data注解就是由Lombok提供的,它会告诉Lombok生成所缺失的方法,同时还会生成......
  • mybatisplus+lombok
       lombok:导入依赖坐标@data包含了除了构造方法之外的所有实体类方法       条件查询:三种方式     查询投影:固定字段 ......