Clean up junk files in Maven library

Clean up junk files in Maven library

Links to related articles:

Maven installation and configuration

Solve the problem that there is no src directory when IDEA creates a maven project

Tips before viewing:

The IDEA version used in this article is ultimate 2019.1, and the JDK version is 1.8.0_141.

When configuring the pom.xml file, you may generate junk files due to your own wrong operation or importing the wrong version of the maven jar package. Here, I wrote a tool class to clean up these junk files.

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();
    }
}

The operation effect is as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43611145/article/details/103580493
Recommended