mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-07 01:28:31 +08:00
release 1.3.1
- 增加编译参数中指定配置类的功能 - 更好地适配 IDEA 部分编译场景
This commit is contained in:
parent
a777f3f90e
commit
a902e803fa
@ -61,7 +61,7 @@ public class User {
|
|||||||
|
|
||||||
```xml
|
```xml
|
||||||
<properties>
|
<properties>
|
||||||
<mapstruct-plus.version>1.2.5</mapstruct-plus.version>
|
<mapstruct-plus.version>1.3.1</mapstruct-plus.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@ -68,6 +68,11 @@ implementation group: 'io.github.linpeilie', name: 'mapstruct-plus-spring-boot-s
|
|||||||
|
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
|
### 1.3.1
|
||||||
|
|
||||||
|
- 增加编译参数中指定配置类的功能
|
||||||
|
- 更好地适配 IDEA 部分编译场景
|
||||||
|
|
||||||
### 1.3.0
|
### 1.3.0
|
||||||
|
|
||||||
- fix: 解决本地开发时 IDEA 编译或者运行时报错等与预期不一致的问题
|
- fix: 解决本地开发时 IDEA 编译或者运行时报错等与预期不一致的问题
|
||||||
|
|||||||
@ -23,6 +23,44 @@ public class MapStructPlusConfiguration {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
:::warning
|
||||||
|
**当使用该方式配置时,强烈建议,在编译参数中,指定配置类为当前类,以解决IDEA部分编译场景时出现的各种问题,该功能从 1.3.1 开始支持**
|
||||||
|
|
||||||
|
配置时,需要在启动参数中添加 `-Amapstruct.plus.mapperConfigClass` 参数,该参数的值为配置类的全路径名称:
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<groupId>io.github.linpeilie</groupId>
|
||||||
|
<artifactId>mapstruct-plus-processor</artifactId>
|
||||||
|
<version>${mapstruct-plus.version}</version>
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok-mapstruct-binding</artifactId>
|
||||||
|
<version>0.2.0</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>-Amapstruct.plus.mapperConfigClass=com.tutelary.MapStructPlusConfiguration</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
```
|
||||||
|
:::
|
||||||
|
|
||||||
-------------------------------------------------------------------
|
-------------------------------------------------------------------
|
||||||
|
|
||||||
除此之外,配置属性还支持**增加编译参数**的方式,以 `-Akey=value` 的形式,传递给编译器。
|
除此之外,配置属性还支持**增加编译参数**的方式,以 `-Akey=value` 的形式,传递给编译器。
|
||||||
@ -31,10 +69,6 @@ public class MapStructPlusConfiguration {
|
|||||||
|
|
||||||
**且使用该方式配置优先级更高**,即,当该方式和配置类同时存在时,以该方式配置的属性为准。该功能从 `1.3.0` 开始支持。
|
**且使用该方式配置优先级更高**,即,当该方式和配置类同时存在时,以该方式配置的属性为准。该功能从 `1.3.0` 开始支持。
|
||||||
|
|
||||||
:::warning
|
|
||||||
**强烈!!!建议使用该方式来配置,因为本地开发时,修改类时,IDEA 会只对修改的类进行部分编译,无法获取到配置类,所以可能会出现与预想编译结果不同的问题,但执行 mvn clean package 又变为正常**。
|
|
||||||
:::
|
|
||||||
|
|
||||||
示例:
|
示例:
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
@ -65,8 +99,7 @@ public class MapStructPlusConfiguration {
|
|||||||
</path>
|
</path>
|
||||||
</annotationProcessorPaths>
|
</annotationProcessorPaths>
|
||||||
<compilerArgs>
|
<compilerArgs>
|
||||||
<arg>-Amapstruct.plus.mapperPackage=com.tutelary.mapper</arg>
|
<arg>-Amapstruct.plus.adapterClassName=DemoConvertMapperAdapter</arg>
|
||||||
<arg>-Amapstruct.plus.adapterClassName=DemoConvertMapperAdapter1</arg>
|
|
||||||
<arg>-Amapstruct.plus.adapterPackage=io.github.linpeilie.adapter</arg>
|
<arg>-Amapstruct.plus.adapterPackage=io.github.linpeilie.adapter</arg>
|
||||||
<arg>-Amapstruct.plus.mapAdapterClassName=DemoMapConvertMapperAdapter</arg>
|
<arg>-Amapstruct.plus.mapAdapterClassName=DemoMapConvertMapperAdapter</arg>
|
||||||
</compilerArgs>
|
</compilerArgs>
|
||||||
|
|||||||
@ -139,4 +139,4 @@ dependencies {
|
|||||||
|
|
||||||
## 本地开发时,修改类后启动报错,mvn clean compile 后又恢复正常
|
## 本地开发时,修改类后启动报错,mvn clean compile 后又恢复正常
|
||||||
|
|
||||||
该问题是由于 IDEA 部分编译导致没有找到配置类导致的,先更新 1.3.0,更换配置方式,详情可以查看[指南 - 配置](/guide/configuration.html)
|
该问题是由于 IDEA 部分编译导致没有找到配置类导致的,先更新最新版本,更换配置方式,详情可以查看[指南 - 配置](/guide/configuration.html)
|
||||||
@ -6,6 +6,11 @@ category:
|
|||||||
description: MapStructPlus release log
|
description: MapStructPlus release log
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### 1.3.1
|
||||||
|
|
||||||
|
- 增加编译参数中指定配置类的功能
|
||||||
|
- 更好地适配 IDEA 部分编译场景
|
||||||
|
|
||||||
### 1.3.0
|
### 1.3.0
|
||||||
|
|
||||||
- fix: 解决本地开发时 IDEA 编译或者运行时报错等与预期不一致的问题
|
- fix: 解决本地开发时 IDEA 编译或者运行时报错等与预期不一致的问题
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<mapstruct.version>1.5.1.Final</mapstruct.version>
|
<mapstruct.version>1.5.1.Final</mapstruct.version>
|
||||||
<mapstruct-plus.version>1.3.0</mapstruct-plus.version>
|
<mapstruct-plus.version>1.3.1</mapstruct-plus.version>
|
||||||
<lombok.version>1.18.22</lombok.version>
|
<lombok.version>1.18.22</lombok.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import java.io.Writer;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.lang.model.element.Modifier;
|
import javax.lang.model.element.Modifier;
|
||||||
|
import javax.lang.model.element.TypeElement;
|
||||||
|
|
||||||
import static javax.tools.Diagnostic.Kind.ERROR;
|
import static javax.tools.Diagnostic.Kind.ERROR;
|
||||||
|
|
||||||
@ -22,6 +23,12 @@ public abstract class AbstractAdapterMapperGenerator {
|
|||||||
public void write(ProcessingEnvironment processingEnv,
|
public void write(ProcessingEnvironment processingEnv,
|
||||||
Collection<AbstractAdapterMethodMetadata> adapterMethods,
|
Collection<AbstractAdapterMethodMetadata> adapterMethods,
|
||||||
String adapterClassName) {
|
String adapterClassName) {
|
||||||
|
final TypeElement typeElement =
|
||||||
|
processingEnv.getElementUtils().getTypeElement(adapterPackage() + "." + adapterClassName);
|
||||||
|
if (typeElement != null) {
|
||||||
|
System.out.println("adapter class existed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
// write Adapter
|
// write Adapter
|
||||||
try (final Writer writer = processingEnv.getFiler()
|
try (final Writer writer = processingEnv.getFiler()
|
||||||
.createSourceFile(adapterPackage() + "." + adapterClassName)
|
.createSourceFile(adapterPackage() + "." + adapterClassName)
|
||||||
|
|||||||
@ -72,6 +72,7 @@ import static io.github.linpeilie.processor.ProcessorOptions.*;
|
|||||||
AUTO_ENUM_MAPPER_ANNOTATION, MAPPER_CONFIG_ANNOTATION, COMPONENT_MODEL_CONFIG_ANNOTATION,
|
AUTO_ENUM_MAPPER_ANNOTATION, MAPPER_CONFIG_ANNOTATION, COMPONENT_MODEL_CONFIG_ANNOTATION,
|
||||||
MAPPER_ANNOTATION})
|
MAPPER_ANNOTATION})
|
||||||
@SupportedOptions({
|
@SupportedOptions({
|
||||||
|
MAPPER_CONFIG_CLASS,
|
||||||
MAPPER_PACKAGE,
|
MAPPER_PACKAGE,
|
||||||
UNMAPPED_SOURCE_POLICY,
|
UNMAPPED_SOURCE_POLICY,
|
||||||
UNMAPPED_TARGET_POLICY,
|
UNMAPPED_TARGET_POLICY,
|
||||||
@ -310,14 +311,10 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
|||||||
AutoMapperProperties.getMapAdapterClassName(), null);
|
AutoMapperProperties.getMapAdapterClassName(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshProperties(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
|
private void loadMapperConfig(MapperConfig mapperConfig) {
|
||||||
// annotation --> MapperConfig
|
if (mapperConfig == null) {
|
||||||
annotations.stream()
|
return;
|
||||||
.filter(this::isMapperConfigAnnotation)
|
}
|
||||||
.findFirst()
|
|
||||||
.flatMap(annotation -> roundEnv.getElementsAnnotatedWith(annotation).stream().findFirst())
|
|
||||||
.ifPresent(element -> {
|
|
||||||
final MapperConfig mapperConfig = element.getAnnotation(MapperConfig.class);
|
|
||||||
AutoMapperProperties.setUnmappedSourcePolicy(mapperConfig.unmappedSourcePolicy());
|
AutoMapperProperties.setUnmappedSourcePolicy(mapperConfig.unmappedSourcePolicy());
|
||||||
AutoMapperProperties.setUnmappedTargetPolicy(mapperConfig.unmappedTargetPolicy());
|
AutoMapperProperties.setUnmappedTargetPolicy(mapperConfig.unmappedTargetPolicy());
|
||||||
AutoMapperProperties.setNullValueMappingStrategy(mapperConfig.nullValueMappingStrategy());
|
AutoMapperProperties.setNullValueMappingStrategy(mapperConfig.nullValueMappingStrategy());
|
||||||
@ -336,7 +333,24 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
|||||||
if (StrUtil.isNotEmpty(mapperConfig.mapAdapterClassName())) {
|
if (StrUtil.isNotEmpty(mapperConfig.mapAdapterClassName())) {
|
||||||
AutoMapperProperties.setMapAdapterClassName(mapperConfig.mapAdapterClassName());
|
AutoMapperProperties.setMapAdapterClassName(mapperConfig.mapAdapterClassName());
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
private void refreshProperties(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
|
||||||
|
// annotation --> MapperConfig
|
||||||
|
annotations.stream()
|
||||||
|
.filter(this::isMapperConfigAnnotation)
|
||||||
|
.findFirst()
|
||||||
|
.flatMap(annotation -> roundEnv.getElementsAnnotatedWith(annotation).stream().findFirst())
|
||||||
|
.ifPresent(element -> loadMapperConfig(element.getAnnotation(MapperConfig.class)));
|
||||||
|
|
||||||
|
// special MapperConfig Class
|
||||||
|
final String mapperConfigClass = processingEnv.getOptions().get(MAPPER_CONFIG_CLASS);
|
||||||
|
if (StrUtil.isNotEmpty(mapperConfigClass)) {
|
||||||
|
final TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(mapperConfigClass);
|
||||||
|
if (typeElement != null) {
|
||||||
|
loadMapperConfig(typeElement.getAnnotation(MapperConfig.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// annotation --> ComponentModelConfig
|
// annotation --> ComponentModelConfig
|
||||||
annotations.stream()
|
annotations.stream()
|
||||||
|
|||||||
@ -9,6 +9,8 @@ import org.mapstruct.ReportingPolicy;
|
|||||||
|
|
||||||
public class ProcessorOptions {
|
public class ProcessorOptions {
|
||||||
|
|
||||||
|
public static final String MAPPER_CONFIG_CLASS = "mapstruct.plus.mapperConfigClass";
|
||||||
|
|
||||||
public static final String DEFAULT_COMPONENT_MODEL = "mapstruct.defaultComponentModel";
|
public static final String DEFAULT_COMPONENT_MODEL = "mapstruct.defaultComponentModel";
|
||||||
|
|
||||||
public static final String MAPPER_PACKAGE = "mapstruct.plus.mapperPackage";
|
public static final String MAPPER_PACKAGE = "mapstruct.plus.mapperPackage";
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import java.io.Writer;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.lang.model.element.Modifier;
|
import javax.lang.model.element.Modifier;
|
||||||
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
|
|
||||||
import static javax.tools.Diagnostic.Kind.ERROR;
|
import static javax.tools.Diagnostic.Kind.ERROR;
|
||||||
@ -19,6 +20,12 @@ import static javax.tools.Diagnostic.Kind.ERROR;
|
|||||||
public class MapperConfigGenerator {
|
public class MapperConfigGenerator {
|
||||||
|
|
||||||
public void write(ProcessingEnvironment processingEnv, String mapstructConfigName, String adapterClassName, List<TypeMirror> uses) {
|
public void write(ProcessingEnvironment processingEnv, String mapstructConfigName, String adapterClassName, List<TypeMirror> uses) {
|
||||||
|
final TypeElement typeElement =
|
||||||
|
processingEnv.getElementUtils().getTypeElement(AutoMapperProperties.getConfigPackage() + "." + adapterClassName);
|
||||||
|
if (typeElement != null) {
|
||||||
|
System.out.println("mapperConfig class existed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
try (final Writer writer = processingEnv.getFiler()
|
try (final Writer writer = processingEnv.getFiler()
|
||||||
.createSourceFile(AutoMapperProperties.getConfigPackage() + "." + mapstructConfigName)
|
.createSourceFile(AutoMapperProperties.getConfigPackage() + "." + mapstructConfigName)
|
||||||
.openWriter()) {
|
.openWriter()) {
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@ -17,7 +17,7 @@
|
|||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<mapstruct-plus.version>1.3.0</mapstruct-plus.version>
|
<mapstruct-plus.version>1.3.1</mapstruct-plus.version>
|
||||||
<maven.compiler.source>8</maven.compiler.source>
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
<maven.compiler.target>8</maven.compiler.target>
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user