maven本地仓库清缓存py脚本

清_remote.repositories、以及 .lastUpdated 缓存文件,避免换仓库or私服的时候一直往旧地方去download从而引起的failtodownlown问题

bash 复制代码
import os
import sys

def delete_maven_metadata_files(directory):
    """
    递归删除指定目录下的 _remote.repositories 和 .lastUpdated 文件
    :param directory: 要扫描的目录(如 Maven 本地仓库路径)
    """
    deleted_files = {"_remote.repositories": 0, ".lastUpdated": 0}

    for root, _, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            # 匹配 _remote.repositories 或以 .lastUpdated 结尾的文件
            if file == "_remote.repositories" or file.endswith(".lastUpdated"):
                try:
                    os.remove(file_path)
                    key = ".lastUpdated" if file.endswith(".lastUpdated") else file
                    deleted_files[key] += 1
                    print(f"已删除: {file_path}")
                except Exception as e:
                    print(f"删除失败 [{file_path}]: {e}")

    print("\n删除完成统计:")
    for file_type, count in deleted_files.items():
        print(f"{file_type}: {count} 个")

if __name__ == "__main__":
    # 硬编码目标目录(修改为你需要的路径)
    target_dir = os.path.expanduser("D:\\dev\\maven-repository2")

    if not os.path.isdir(target_dir):
        print(f"错误: 目录不存在 [{target_dir}]")
        sys.exit(1)

    print(f"开始清理目录: {target_dir}")
    delete_maven_metadata_files(target_dir)
相关推荐
用户8307196840825 小时前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp
NE_STOP3 天前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
spring
JavaGuide4 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
玹外之音4 天前
Spring AI MCP 实战:将你的服务升级为 AI 可调用的智能工具
spring·ai编程
来一斤小鲜肉4 天前
Spring AI入门:第一个AI应用跑起来
spring·ai编程
NE_STOP4 天前
springMVC-常见视图组件与RESTFul编程风格
spring
what丶k5 天前
Spring AI 多模态开发全解析:从入门到企业级落地
后端·spring·ai编程
NE_STOP5 天前
springMVC-获取前端请求的数据与三个作用域
spring
莫寒清5 天前
Spring MVC:@PathVariable 注解详解
java·spring·mvc