根据pom文件指定依赖在本地repository复制到新建文件夹

xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT、编程案例、资料请联系QQ:1280023003百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!


该功能的实现主要是便于maven项目或者web的jar引入,能够快速通过pom文件找到需要的jar包。指定要输出的路径之后,该功能会直接通过pom文件寻找repository,并复制。最终导入项目就不需要一个一个去网上下载了。功能还是比较简单的,直接上代码。

package com.echo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FileUtils {

    public static void main(String[] args) throws Exception {
      
    }

    public static Set<String> method() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("D:\\CodeWork\\myeclipse\\code1\\com-echo-basic\\pom.xml"));

        Set<String> listName = new HashSet<>();

        //获取文档根节点
        Element root = document.getRootElement();
        //获取根节点下面的所有子节点(不包过子节点的子节点)
        List<Element> list = root.elements();
        //遍历List的方法
        for (Element e:list){
            if(e.getName().equals("dependencies")){
                List<Element> elements = e.elements();
                for (Element e1 : elements){
                    if(e1.getName().equals("dependency")){
                        List<Element> elements1 = e1.elements();
                        StringBuffer sb = new StringBuffer();
                        for (Element e2 : elements1){
                            if(e2.getName().equals("artifactId")){
                                sb.append(e2.getData());
                            }
                            if(e2.getName().equals("version")){
                                sb.append("-").append(e2.getData());
                            }
                            listName.add(sb.toString()+".jar");
                        }
                    }
                }
            }
        }
        return listName;
    }

    public static void fileCopy() throws DocumentException {
        List<File> getallfile = getallfile("D:\\develop\\apache-localRepository");
        Set<String> method = method();
        Integer num = 0;
        for (File file : getallfile) {
            if (file.getName().endsWith(".jar")) {
                if(method.contains(file.getName())){
                    num ++;
                    copyFileReaderToWriterChar(file.getAbsolutePath(), "D:\\develop\\repositoryJar\\" + file.getName());
                }
            }
        }
        System.out.println(num);
    }

    public static List<File> getallfile(String path) {
        List<File> allfilelist = new ArrayList<File>();
        return getallfile(new File(path), allfilelist);
    }

    public static List<File> getallfile(File file, List<File> allfilelist) {
        if (file.exists()) {
            //判断文件是否是文件夹,如果是,开始递归
            if (file.isDirectory()) {
                File f[] = file.listFiles();
                for (File file2 : f) {
                    getallfile(file2, allfilelist);
                }
            } else {
                allfilelist.add(file);
            }
        }
        return allfilelist;
    }

    /**
     * 字符流方式复制文件,一次复制一个字节
     * @param srcFilePath 源文件路径
     * @param destFilePath 新文件路径
     */
    public static void copyFileReaderToWriterChar(String srcFilePath,String destFilePath){
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try{
            fileReader = new FileReader(srcFilePath);
            fileWriter = new FileWriter(destFilePath);
            int charRead;
            while((charRead = fileReader.read()) != -1){
                fileWriter.write(charRead);
            }
            fileWriter.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != fileReader){
                try{
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != fileWriter){
                try{
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xlecho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值