fixed: 和lombok以及mapstruct同时使用时,APT配置文件不生效 close #I6WTN6

This commit is contained in:
开源海哥 2023-04-19 10:06:58 +08:00
parent 42c59e87d3
commit 930f796812
4 changed files with 67 additions and 62 deletions

View File

@ -16,58 +16,40 @@
package com.mybatisflex.processor;
import javax.annotation.processing.Filer;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.*;
import java.net.URL;
import java.util.Properties;
class MyBatisFlexProps {
protected Properties properties = null;
protected Properties properties = new Properties();
private static final String DEFAULT_ENCODING = "UTF-8";
public MyBatisFlexProps(String fileName) {
this(fileName, DEFAULT_ENCODING);
}
public MyBatisFlexProps(String fileName, String encoding) {
properties = new Properties();
public MyBatisFlexProps(Filer filer) {
InputStream inputStream = null;
try {
inputStream = getResourceAsStreamByCurrentThread(fileName);
FileObject propertiesFileObject = filer.getResource(StandardLocation.CLASS_OUTPUT, ""
, "mybatis-flex.properties");
if (inputStream == null) {
inputStream = getResourceAsStreamByClassloader(fileName);
}
// 当系统未编译的时候开发环境下的 resources 目录下的 properties 文件不会自动被 copy target/classes 目录下
// 此时需要主动去探测 resources 目录的文件
if (inputStream == null) {
URL resourceURL = MyBatisFlexProps.class.getResource("/");
if (resourceURL != null) {
String classPath = resourceURL.toURI().getPath();
if (removeSlashEnd(classPath).endsWith("classes")) {
//from classes path
File propFile = new File(classPath, fileName);
if (propFile.exists() && propFile.isFile()) {
inputStream = new FileInputStream(propFile);
}
//from resources path
else {
File resourcesDir = new File(classPath, "../../src/main/resources");
propFile = new File(resourcesDir, fileName);
if (propFile.exists() && propFile.isFile()) {
inputStream = new FileInputStream(propFile);
}
}
}
File propertiesFile = new File(propertiesFileObject.toUri());
if (propertiesFile.exists()) {
inputStream = propertiesFileObject.openInputStream();
} else {
File pomXmlFile = new File(propertiesFile.getParentFile().getParentFile().getParentFile(), "pom.xml");
if (pomXmlFile.exists()) {
propertiesFile = new File(pomXmlFile.getParentFile(), "src/main/resources/mybatis-flex.properties");
}
}
if (inputStream == null && propertiesFile.exists()) {
inputStream = new FileInputStream(propertiesFile);
}
if (inputStream != null) {
properties.load(new InputStreamReader(inputStream, encoding));
properties.load(new InputStreamReader(inputStream, DEFAULT_ENCODING));
}
} catch (Exception e) {
} finally {
@ -80,27 +62,6 @@ class MyBatisFlexProps {
}
}
private InputStream getResourceAsStreamByCurrentThread(String fileName) {
ClassLoader ret = Thread.currentThread().getContextClassLoader();
return ret != null ? ret.getResourceAsStream(fileName) : null;
}
private InputStream getResourceAsStreamByClassloader(String fileName) {
ClassLoader ret = MyBatisFlexProps.class.getClassLoader();
return ret != null ? ret.getResourceAsStream(fileName) : null;
}
private static String removeSlashEnd(String path) {
if (path != null && (path.endsWith("/") || path.endsWith("\\"))) {
return path.substring(0, path.length() - 1);
} else {
return path;
}
}
public Properties getProperties() {
return properties;
}

View File

@ -30,8 +30,7 @@ import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.tools.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
@ -118,7 +117,7 @@ public class QueryEntityProcessor extends AbstractProcessor {
if (!roundEnv.processingOver()) {
MyBatisFlexProps props = new MyBatisFlexProps("mybatis-flex.properties");
MyBatisFlexProps props = new MyBatisFlexProps(filer);
String enable = props.getProperties().getProperty("processor.enable", "");
if ("false".equalsIgnoreCase(enable)) {

View File

@ -60,4 +60,48 @@
</dependency>
</dependencies>
<build>
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>src/main/resources</directory>-->
<!-- <includes>-->
<!-- <include>**/*</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- </resources>-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<!-- <path>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <version>${org.projectlombok.version}</version>-->
<!-- </path>-->
<!-- <path>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok-mapstruct-binding</artifactId>-->
<!-- <version>${lombok-mapstruct-binding.version}</version>-->
<!-- </path>-->
<!-- <path>-->
<!-- <groupId>org.mapstruct</groupId>-->
<!-- <artifactId>mapstruct-processor</artifactId>-->
<!-- <version>${org.mapstruct.version}</version>-->
<!-- </path>-->
<path>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>1.1.5</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1 @@
#processor.mappersGenerateEnable = false