具有 groovy 类 的 MapStruct
MapStruct with groovy classes
我想在 groovy 类 和 gradle 上使用 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 类 一起工作以及我必须如何配置它?
所以你可以使用这个版本:
plugins {
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
compileGroovy.groovyOptions.javaAnnotationProcessing = true
有了这个Car
(从他们的例子中稍微修改)
import groovy.transform.Immutable
@Immutable
class Car {
String make;
int numberOfSeats;
CarType type;
}
还有这个CarDto
(同样,稍作修改)
import groovy.transform.ToString
@ToString
class CarDto {
String make;
int seatCount;
String type;
}
那么您需要在 CarMapper
中进行的唯一更改是忽略 Groovy 添加到对象的 metaClass
属性:
import org.mapstruct.Mapper
import org.mapstruct.Mapping
import org.mapstruct.factory.Mappers
@Mapper
interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper)
@Mapping(source = "numberOfSeats", target = "seatCount")
@Mapping(target = "metaClass", ignore = true)
CarDto carToCarDto(Car car)
}
然后你可以这样做:
class Main {
static main(args) {
Car car = new Car("Morris", 5, CarType.SEDAN);
CarDto carDto = CarMapper.INSTANCE.carToCarDto(car);
println carDto
}
}
打印出:
main.CarDto(Morris, 5, SEDAN)
我想在 groovy 类 和 gradle 上使用 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 类 一起工作以及我必须如何配置它?
所以你可以使用这个版本:
plugins {
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
compileGroovy.groovyOptions.javaAnnotationProcessing = true
有了这个Car
(从他们的例子中稍微修改)
import groovy.transform.Immutable
@Immutable
class Car {
String make;
int numberOfSeats;
CarType type;
}
还有这个CarDto
(同样,稍作修改)
import groovy.transform.ToString
@ToString
class CarDto {
String make;
int seatCount;
String type;
}
那么您需要在 CarMapper
中进行的唯一更改是忽略 Groovy 添加到对象的 metaClass
属性:
import org.mapstruct.Mapper
import org.mapstruct.Mapping
import org.mapstruct.factory.Mappers
@Mapper
interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper)
@Mapping(source = "numberOfSeats", target = "seatCount")
@Mapping(target = "metaClass", ignore = true)
CarDto carToCarDto(Car car)
}
然后你可以这样做:
class Main {
static main(args) {
Car car = new Car("Morris", 5, CarType.SEDAN);
CarDto carDto = CarMapper.INSTANCE.carToCarDto(car);
println carDto
}
}
打印出:
main.CarDto(Morris, 5, SEDAN)