本来想用xjar混淆jar呢,结果一直下不下来,应该是本地有着.lastUpdated文件夹,但是又懒得删除,写个工具类方便日后使用
我自己试了下,把我的maven玩崩了哈哈哈,使用之前一定一定一定要备份仓库中org.apache.maven
目录
查到一个bat命令还没试 来源Cannot resolve Plugin org.apache.maven.plugins:maven-install-plugin
set REPOSITORY_PATH=D:\apache-maven-3.8.5\repositry
rem 正在搜索...
for /f "delims=" %%i in ('dir /b /s "%REPOSITORY_PATH%\*lastUpdated*"') do (
del /s /q %%i
)
rem 搜索完毕
pause
package net.lesscoding.test;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author eleven
* @date 2023/6/30 11:29
* @apiNote
*/
public class ClearTest {
@Test
public void clearLastUpdated() {
String rootPath = "E:/maven/repository";
Map<File, Boolean> resultMap = traverseDirectory(rootPath);
resultMap.forEach((k,v) -> System.out.println(
String.format("当前文件 %s, 不含有jar包或文件夹名字含有unknown - %b", k, v)
));
processMap(resultMap);
}
private static Map<File, Boolean> traverseDirectory(String rootPath) {
Map<File, Boolean> map = new LinkedHashMap<>();
try {
Files.walkFileTree(Paths.get(rootPath), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
File currentDir = dir.toFile();
if (currentDir.getName().contains("unknown")) {
map.put(currentDir, true);
return FileVisitResult.SKIP_SUBTREE;
}
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
File currentFile = file.toFile();
if (currentFile.getName().endsWith(".lastUpdated")) {
File parentDir = currentFile.getParentFile();
if (hasJarFile(parentDir)) {
map.put(currentFile, false);
} else {
map.put(currentFile, true);
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
private static boolean hasJarFile(File directory) {
File[] files = directory.listFiles((dir, name) -> name.endsWith(".jar"));
return files != null && files.length > 0;
}
private static void processMap(Map<File, Boolean> map) {
System.out.println("=============开始删除文件=============");
AtomicLong totalDiskSpaceFreed = new AtomicLong();
AtomicLong deletedFilesCount = new AtomicLong();
map.forEach((key,value) -> {
if (value) {
File parentFile = key.getParentFile();
if (null != parentFile) {
totalDiskSpaceFreed.addAndGet(parentFile.length());
deletedFilesCount.getAndIncrement();
deleteUselessFile(parentFile);
}
} else {
totalDiskSpaceFreed.addAndGet(key.length());
deletedFilesCount.getAndIncrement();
deleteUselessFile(key);
}
});
System.out.println(String.format("共删除文件/目录 %d 个,清理空间%d Byte", deletedFilesCount.get(), totalDiskSpaceFreed.get()));
System.out.println("=============删除文件完成=============");
}
private static void deleteUselessFile(File file) {
if (file.isDirectory()) {
try {
Files.walk(Paths.get(file.getAbsolutePath()))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (Exception e) {
e.printStackTrace();
}
}
// 删除文件
file.delete();
System.out.println(String.format("当前删除文件 %s 大小 %d", file.getName(), file.length()));
}
}