清理Maven库中垃圾文件
相关文章链接:
观前提示:
本文所使用的IDEA版本为ultimate 2019.1,JDK版本为1.8.0_141。
在配置pom.xml文件时,可能会因为自己的错误操作或者是导入错误的maven jar包的版本导致生成垃圾文件,这里,自己写的一个工具类来清理这些垃圾文件。
package cleanMavenRepository;
import java.io.File;
/**
* @Decription 清理Maven仓库中的垃圾文件
* @Create 2019-12-17 by jjy
*/
public class CleaRepositoryUtil {
public static void main(String[] args) {
String mavenRepositoryPath = "D:\\Program Files\\Apache\\maven-repository";
System.out.println("Maven仓库路径为:" + mavenRepositoryPath);
File file= new File(mavenRepositoryPath);
System.out.println("开始清除Maven仓库空文件夹");
clean(file);
System.out.println("------------------------------------------------");
System.out.println("清理成功");
}
/**
* @Decription 递归清理垃圾文件
* @param file
* @return
* @Create 2019-12-17 by jjy
*/
private static boolean clean(File file){
boolean haveFile = false;
File[] files = file.listFiles();
if(files.length > 0 ){
for(File f: files){
//判断是否有文件夹
if(f.isDirectory()){
boolean childHaveFile = clean(f);
if(childHaveFile){
haveFile = true;
}
}
//判断是否含有jar文件
if(f.isFile() && f.getName().endsWith(".jar")){
haveFile = true;
}
}
}
if(!haveFile){
deleteFile(file);
}
return haveFile;
}
/**
* @Decription 删除文件夹及其中文件
* @param file
* @Create 2019-12-17 by jjy
*/
private static void deleteFile(File file){
System.out.println("------------------------------------------------");
File[] files = file.listFiles();
if(files.length > 0){
for (File f: files){
System.out.println("删除文件:" + f.getAbsolutePath());
f.delete();
}
}
System.out.println("删除文件夹:" + file.getAbsolutePath());
file.delete();
}
}
运行效果如下