清除本地maven仓库下载失败的记录工具类

本来想用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()));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值