【发布时间】:2021-11-21 08:30:10
【问题描述】:
我想在带有 gradle 的 groovy 类上使用 Mapstruct 映射器。 build.gradle 中的配置类似于 Java 项目。
dependencies {
...
compile 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' // if you are using mapstruct in test code
}
问题是没有生成映射器的实现类。 我还尝试为 groovy 编译任务应用不同的选项,但它不起作用。
compileGroovy {
options.annotationProcessorPath = configurations.annotationProcessor
// if you need to configure mapstruct component model
options.compilerArgs << "-Amapstruct.defaultComponentModel=default"
options.setAnnotationProcessorGeneratedSourcesDirectory( file("$projectDir/src/main/generated/groovy"))
}
有谁知道 Mapstruct 是否可以与 groovy 类一起使用以及我必须如何配置它?
【问题讨论】:
-
为什么在 Groovy 中需要 MapStruct?你的用例是什么?
-
不同对象之间的映射,能够在 MapStruct 注释中指定一些特定的映射规则。一个例子可以是类中不同的属性名称,或者有时我必须忽略空属性