!26 fix: CleanTask 将 build 目录删除导致无法将字节码写入目标文件

Merge pull request !26 from zhangxun/bugfix/cli-clean-and-compile-run
This commit is contained in:
Luke 2025-07-05 02:55:17 +00:00 committed by Gitee
commit 00afda56bb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -30,13 +30,15 @@ public final class CleanTask implements Task {
*/ */
@Override @Override
public void run() throws IOException { public void run() throws IOException {
deleteDir(Path.of("build")); deleteDir(Path.of("build"), false);
deleteDir(Path.of("dist")); deleteDir(Path.of("dist"), false);
System.out.println("[clean] done."); System.out.println("[clean] done.");
} }
/** /**
* 递归删除指定目录及其所有子文件和子目录 * 递归删除指定目录下的所有子文件和子目录
* 如需删除指定目录本身可将第二个参数 {@code containSelf} 设置为 true
* <p> * <p>
* 若目录不存在则直接返回 * 若目录不存在则直接返回
* </p> * </p>
@ -45,14 +47,18 @@ public final class CleanTask implements Task {
* </p> * </p>
* *
* @param dir 需要删除的目录路径 * @param dir 需要删除的目录路径
* @param containSelf 是否删除指定目录本身
* @throws IOException 删除目录或文件过程中发生 IO 错误时抛出 * @throws IOException 删除目录或文件过程中发生 IO 错误时抛出
*/ */
private void deleteDir(Path dir) throws IOException { private void deleteDir(Path dir, boolean containSelf) throws IOException {
if (Files.notExists(dir)) return; if (Files.notExists(dir)) return;
try (var stream = Files.walk(dir)) { try (var stream = Files.walk(dir)) {
stream.sorted(Comparator.reverseOrder()) // 先删子文件后删父目录 stream.sorted(Comparator.reverseOrder()) // 先删子文件后删父目录
.forEach(p -> { .forEach(p -> {
try { try {
if (!containSelf && p == dir) {
return;
}
Files.delete(p); Files.delete(p);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);