码迷,mamicode.com
首页 > 编程语言 > 详细

【SpringCloud-Maven】依赖版本比对

时间:2020-11-17 12:40:40      阅读:9      评论:0      收藏:0      [点我收藏+]

标签:ast   hashcode   dex   好用   end   ash   EDA   字节   app   

 

在pom文件中,依赖的jar文件非常多,如果有人改了仓库,例如上传jar文件中断导致字节丢失,删jar、更改版本等,会导致项目无法正常启动,

虽然我们没有改动pom文件,但是由于他人的行为,我们很难排查出来是哪个jar出了问题,那么,我们可以将新打包和旧的jar文件进行解压,提取里面的

lib目录,进行jar比对,包括名称、版本、大小及修改时间。

在网上搜索了一阵发现没有现成好用的工具,于是写了这个软件工具。方便排查。

 

入参:两个lib目录

例如:C:\\Users\\o\\Desktop\\lib0",    "C:\\Users\\o\\Desktop\\lib2

技术图片

 

 

 

输出为比对结果log文件。comprare.log

 

技术图片

 

 

 

 

package com.clb.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class MyTest022 {

    public File[] getFileList(String dirPath) {
        File dirFile = new File(dirPath);
        if (!dirFile.exists() || !dirFile.isDirectory() || !dirFile.canRead()) {
            throw new RuntimeException("file(" + dirPath + ")can not exist、un-directory or unread.");
        }
        return dirFile.listFiles();
    }

    public StringBuilder compare(String dir1, String dir2) {
        File[] files1 = getFileList(dir1);
        Map<String, SimpleFile> map1 = new HashMap<>(); //all jar
        List<File> list1_error = new ArrayList<>(); //log
        for (int i = 0; i < files1.length; i++) {
            if (isJarFile(files1[i])) {
                SimpleFile sf = new SimpleFile(files1[i]);
                map1.put(sf.name, sf);
            } else {
                list1_error.add(files1[i]);
            }
        }

        File[] files2 = getFileList(dir2);
        Map<String, SimpleFile> map2 = new HashMap<>(); //all jar
        List<File> list2_error = new ArrayList<>(); //log
        for (int i = 0; i < files2.length; i++) {
            if (isJarFile(files2[i])) {
                SimpleFile sf = new SimpleFile(files2[i]);
                map2.put(sf.name, sf);
            } else {
                list2_error.add(files2[i]);
            }
        }

        Map<String, List<SimpleFile>> result = new TreeMap<>((o1, o2) -> o1.compareTo(o2));
        for (String name1 : map1.keySet()) {
            SimpleFile sf = map1.get(name1);
            if (map2.keySet().contains(name1)) {
                if (!map2.values().contains(sf)) {
                    result.put("1_"+name1, Arrays.asList(sf, map2.get(name1)));
                } else {
                    result.put("0_"+name1, Arrays.asList(sf, sf));
                }
            } else {
                result.put("2_"+name1, Arrays.asList(sf, null));
            }
        }

        for (String name2 : map2.keySet()) {
            if (!map1.keySet().contains(name2)){
                result.put("2_"+name2, Arrays.asList(null, map2.get(name2)));
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuilder stringBuilder = new StringBuilder("|序号|--——---|名 称|-------------------------|比对结果|---------|源目录文件信息[版本、大小、更新时间]|-----------------------|目标文件信息[版本、大小、更新时间]|-----------\n");


        int count = 0;
        for (String name : result.keySet()) {
            List<SimpleFile> list = result.get(name);
            SimpleFile sf = list.get(0);
            String ne = name.substring(2);
            count++;
            if (name.startsWith("0_")) {
                stringBuilder.append(format(" "+count, 10) + format(ne, 38) + format("[相同]", 18) + format(sf.version, 18) + format(sf.size+"B", 13) + format(sdf.format(new Date(sf.update)), 27));
            } else if (name.startsWith("1_")) {
                SimpleFile sf2 = list.get(1);
                stringBuilder.append(format(" "+count, 10) + format(ne, 38) + format("[不相同]", 17) + format(sf.version, 18) + format(sf.size+"B", 13) + format(sdf.format(new Date(sf.update)), 30)
                + format(sf2.version, 18) + format(sf2.size+"B", 13) + format(sdf.format(new Date(sf2.update)), 27));
            } else if (name.startsWith("2_")){
                if (sf == null) {
                    SimpleFile sf2 = list.get(1);
                    stringBuilder.append(format(" "+count, 10) + format(ne, 38) + format("[不相同]", 17) + format("不存在", 58) + format(sf2.version, 18) + format(+sf2.size+"B", 13) + format(sdf.format(new Date(sf2.update)), 27));
                } else {
                    stringBuilder.append(format(" "+count, 10) + format(ne, 38) + format("[不相同]", 17) + format(sf.version, 18) + format(sf.size+"B", 13) + format(sdf.format(new Date(sf.update)), 27) + format("不存在", 30));
                }
            } else {
                stringBuilder.append(ne+"这是什么玩意>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
            stringBuilder.append("\n");
        }

        return stringBuilder;
    }

    private String format(String s, int len) {
        if (s == null) s = "";
        if (s.length() >= len) {
            return s;
        }
        return format(s+" ", len);
    }

    public static void main(String[] args) throws IOException {
        MyTest022 test022 = new MyTest022();
        StringBuilder log = test022.compare("C:\\Users\\o\\Desktop\\lib0", "C:\\Users\\o\\Desktop\\lib2");
        Files.write(Paths.get("C:\\Users\\o\\Desktop\\comprare.log"),log.toString().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
    }

    public static boolean isJarFile(File file) {
        if (file.exists() && file.isFile() && file.getName().length() > 7) {
            return file.getName().toLowerCase().endsWith(".jar")
                    && file.getName().indexOf("-") > 0;
        }
        return false;
    }

    class SimpleFile {
        String name;
        String version;
        long size;
        long update;
        public SimpleFile(File file) {
            int index = file.getName().lastIndexOf("-");
            this.name = file.getName().substring(0, index);
            this.version = file.getName().substring(index+1, file.getName().length()-4);
            this.size = file.length();
            this.update = file.lastModified();
        }
        public int hashCode(){
            return this.toString().hashCode();
        }
        @Override
        public boolean equals(Object o) {
            return this.toString().equals(o.toString());
        }
        public String toString() {
            return new StringBuilder(name).append(version).append(size).append(update).toString();
        }
    }
}

 

【SpringCloud-Maven】依赖版本比对

标签:ast   hashcode   dex   好用   end   ash   EDA   字节   app   

原文地址:https://www.cnblogs.com/cheng2839/p/13960663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!