我使用 Eclipse
IDE 开发了一个使用 Mapstruct
的应用程序,现在我正转向 IntelliJ
继续开发。
在 Eclipse 上一切正常,但由于使用了 annotationProcessorPaths
,我在 IntelliJ 上出现了意外行为。
我的配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=${mapstruct.defaultComponentModel}
</compilerArg>
<compilerArg>
-Aorg.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
</compilerArg>
</compilerArgs>
</configuration>
在 IntelliJ 上,当我启动 Maven 全新安装时,我得到了生成的源代码:
@Component
public class FieldMapperImpl implements FieldMapper {
@Autowired
private FieldMapperResolver fieldMapperResolver;
...
}
但是当我运行/调试我的 Spring Boot 应用程序时,我得到的生成源是:
public class FieldMapperImpl implements FieldMapper {
private final FieldMapperResolver fieldMapperResolver = new FieldMapperResolver();
...
}
我该如何解决这个问题?
最佳答案
我假设您正在直接通过 IntelliJ 运行/调试 Spring Boot 应用程序。发生这种情况的原因是因为 IntelliJ 不会从 maven-compiler-plugin
中获取配置。参见 IDEA-143742和 IDEA-150621 .
您还必须单独配置 IntelliJ 注释处理器选项。你可以在 Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors 中找到它
奇怪的是 IntelliJ 甚至如何调用处理器,你的 pom 中是否也有 mapstruct-processor
作为依赖项?
编辑:
IntelliJ 不获取 maven-compiler-plugin 编译器参数。默认组件模型是通过注释处理器选项设置的。为了使 IntelliJ 正常工作,应该在 IntelliJ 配置中设置相同的属性或使用 @Mapper(componentModel = "spring")
。更多信息在 documentation
关于maven - 映射结构 : generated sources in intelliJ using annotationProcessorPaths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43585890/