优化APT代码路径
This commit is contained in:
@@ -12,7 +12,9 @@ import javax.tools.JavaFileObject;
|
|||||||
import javax.tools.StandardJavaFileManager;
|
import javax.tools.StandardJavaFileManager;
|
||||||
import javax.tools.StandardLocation;
|
import javax.tools.StandardLocation;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class CompilerUtils {
|
public class CompilerUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,9 +98,9 @@ public class CompilerUtils {
|
|||||||
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
|
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
|
||||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(
|
||||||
entityPath);
|
entityPath);
|
||||||
// // 设置注解处理器
|
// 设置注解处理器
|
||||||
Iterable<? extends Processor> processors = List.of(new MybatisFlexProcessor());
|
Iterable<? extends Processor> processors = List.of(new MybatisFlexProcessor());
|
||||||
// // 添加 -proc:only 选项
|
// 添加 -proc:only 选项
|
||||||
List<String> options = List.of("-proc:only");
|
List<String> options = List.of("-proc:only");
|
||||||
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options,
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options,
|
||||||
null, compilationUnits);
|
null, compilationUnits);
|
||||||
@@ -106,27 +108,35 @@ public class CompilerUtils {
|
|||||||
task.call();
|
task.call();
|
||||||
compilationUnits = fileManager.getJavaFileObjects(
|
compilationUnits = fileManager.getJavaFileObjects(
|
||||||
javaPathList.toArray(new String[0]));
|
javaPathList.toArray(new String[0]));
|
||||||
// 需在entity之后加载
|
|
||||||
// 设置编译输出目录
|
// 设置编译输出目录
|
||||||
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(new File("target/classes")));
|
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(new File("target/classes")));
|
||||||
|
|
||||||
task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
|
task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
|
||||||
String pathStr = actModulePath + File.separator + "entity" + File.separator + "table" + File.separator;
|
String pathStr = actModulePath + File.separator + "entity" + File.separator + "table" + File.separator;
|
||||||
String filePathStr = pathStr + fileName + "EntityTableDef.java";
|
String filePathStr = pathStr + fileName + "EntityTableDef.java";
|
||||||
|
// 需在entity之后加载
|
||||||
javaPathList.add(1, filePathStr);
|
javaPathList.add(1, filePathStr);
|
||||||
boolean success = task.call();
|
boolean success = task.call();
|
||||||
if (success) {
|
if (success) {
|
||||||
System.out.println("Compilation and annotation processing completed successfully.");
|
System.out.println("Compilation and annotation processing completed successfully.");
|
||||||
String commPath = PathUtils.getUserDir() + File.separator + "com";
|
// 指定源文件夹和目标文件夹
|
||||||
if (countFiles(new File(commPath)) <= 1) {
|
File sourceDir = new File("com");
|
||||||
FileUtil.clean(commPath);
|
File destinationDir = new File(PathUtils.getTargetGeneratedAnnotations());
|
||||||
FileUtil.del(commPath);
|
// 确保目标文件夹存在
|
||||||
|
destinationDir.mkdirs();
|
||||||
|
// 移动源文件夹内容到目标文件夹
|
||||||
|
if (sourceDir.exists()) {
|
||||||
|
FileUtil.move(sourceDir, destinationDir, true);
|
||||||
|
}
|
||||||
|
if (countFiles(sourceDir) <= 1) {
|
||||||
|
FileUtil.clean(sourceDir);
|
||||||
|
FileUtil.del(sourceDir);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Compilation and annotation processing failed.");
|
System.out.println("Compilation and annotation processing failed.");
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("compilerEntityTableDefError", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private static int countFiles(File directory) {
|
private static int countFiles(File directory) {
|
||||||
|
|||||||
@@ -18,13 +18,20 @@ public class PathUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getModulesPath() {
|
public static String getModulesPath() {
|
||||||
return getUserDir() + File.separator + "src"+ File.separator + "main" + File.separator + "java" + File.separator + CoolApplication.class.getPackageName()
|
return getUserDir() + getSrcMainJava() + File.separator + CoolApplication.class.getPackageName()
|
||||||
.replace(".", File.separator) + File.separator + "modules";
|
.replace(".", File.separator) + File.separator + "modules";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getSrcMainJava() {
|
||||||
|
return File.separator + "src" + File.separator + "main" + File.separator + "java";
|
||||||
|
}
|
||||||
|
public static String getTargetGeneratedAnnotations() {
|
||||||
|
return "target" + File.separator + "generated-sources" + File.separator + "annotations";
|
||||||
|
}
|
||||||
|
|
||||||
public static String getClassName(String filePath) {
|
public static String getClassName(String filePath) {
|
||||||
// 定位 "src/main/java" 在路径中的位置
|
// 定位 "src/main/java" 在路径中的位置
|
||||||
int srcMainJavaIndex = filePath.indexOf("src" + File.separator + "main" + File.separator + "java");
|
int srcMainJavaIndex = filePath.indexOf(getSrcMainJava());
|
||||||
if (srcMainJavaIndex == -1) {
|
if (srcMainJavaIndex == -1) {
|
||||||
throw new IllegalArgumentException("File path does not contain 'src/main/java'");
|
throw new IllegalArgumentException("File path does not contain 'src/main/java'");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user