多模块Maven项目中MapStruct生成类随机出现编译错误的问题求助
多模块Maven项目中MapStruct生成类随机出现编译错误的问题求助
我现在维护一个采用六边形架构的Maven多模块项目,项目结构如下:
- parent
- domain(依赖Lombok)
- app(依赖Lombok)
- infra(聚合POM)
- output-adapters(依赖MapStruct + Lombok,依赖rest-api、bbdd、domain模块)
- rest-api(依赖OpenAPI Generator + MapStruct + Lombok,依赖domain模块)
- bbdd(依赖QueryDSL + Lombok)
在output-adapters模块里,MapStruct生成的实现类经常出现编译错误。比如生成的*Impl类乍一看代码结构没问题,但运行时会抛出包含一堆未解决编译问题的Error,具体错误如下:
@Component public class WorklogEntityMapperImpl { @Autowired private UserEntityMapper userEntityMapper; @Autowired private BusinessEntityMapper businessEntityMapper; public WorklogEntityMapperImpl() { throw new Error("Unresolved compilation problems: \n\tWorklogEntityMapper cannot be resolved to a type\n\tUserEntityMapper cannot be resolved to a type\n\tBusinessEntityMapper cannot be resolved to a type\n\tThe method domain2Infra(Worklog) of type WorklogEntityMapperImpl must override or implement a supertype method\n\tUserEntityMapper cannot be resolved to a type\n\tBusinessEntityMapper cannot be resolved to a type\n\tThe method infra2domain(WorklogEntity) of type WorklogEntityMapperImpl must override or implement a supertype method\n\tUserEntityMapper cannot be resolved to a type\n\tBusinessEntityMapper cannot be resolved to a type\n\tThe method finaMapping(WorklogEntity, Worklog.WorklogBuilder) is undefined for the type WorklogEntityMapperImpl\n\tThe method infra2domain(Iterable<WorklogEntity>) of type WorklogEntityMapperImpl must override or implement a supertype method\n\tThe method domain2Infra(WorklogFilteredRequest) of type WorklogEntityMapperImpl must override or implement a supertype method\n"); } // 其他实现代码... }
这个问题非常诡异:
- 只有
output-adapters模块出问题,bbdd和rest-api模块的MapStruct生成类从来都是正常编译运行的 - 错误出现完全随机:有时候执行
mvn clean verify能顺利完成,但完全相同的命令换个时机执行就会报上面的错误 - 一旦触发这个问题,我得反复尝试清理缓存、重启IDE、重新编译等操作才能恢复,本地开发环境根本没法稳定启动应用
- 但GitHub Actions上的测试、集成扫描和Sonar分析从来没出过这个问题,Docker部署也一切正常
下面是我的Maven配置细节:
我在父POM中统一配置了maven-compiler-plugin,让所有模块继承该配置,内容如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <useIncrementalCompilation>false</useIncrementalCompilation> <compilerArgs> <!-- 让MapStruct生成Spring Bean类型的实现类 --> <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg> <compilerArg>-Amapstruct.verbose=true</compilerArg> </compilerArgs> <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>${lombok.version}</version> </path> <!-- Lombok 1.8.16+版本需要该绑定依赖 --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>${lombok-mapstruct-binding.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin>
另外,针对bbdd模块单独配置了QueryDSL相关的注解处理器:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <!-- QueryDSL --> <path> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <version>${querydsl.version}</version> <classifier>jakarta</classifier> </path> <!-- JPA元模型 --> <path> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>${jakarta-persistence.version}</version> </path> <!-- Lombok --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
我已经尝试过清理Maven本地仓库、关闭增量编译、重启IDE等操作,但问题还是会随机出现。有没有大佬遇到过类似的情况?或者能帮我分析下可能的根本原因?
内容来源于stack exchange
相关产品推荐
相关产品推荐

