工具类 从jar包里复制要打生产的补丁(仅限T2)

package com.bytter.ext.test;

import com.bytter.core.utils.ZipUtil;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class CopyT2ClassAndResouce {

    //项目地址
    static String src="src_50764";
    static String projectPath=("D:\\software\\workspace\\t2\\fujian_haokelai\\code\\"+src).replaceAll("\\\\", "/");
    static String exportBasePath="E:\\bug单\\T2-华中部\\福建-豪客来\\补丁".replaceAll("\\\\", "/")+ File.separator+new SimpleDateFormat("yyyyMMddHHmm").format(new Date());

    static Map<String,String> jarPathMap = new HashMap<>();
    static Map<String,Boolean> jarUnZipMap = new HashMap<>();
    static String extensionName = "bt-biz-extension";
    static String btWeb = "bt-web";
    static List<String> ignoreList = new ArrayList<>();
    static List<String> noExistsList = new ArrayList<>();



    /**
     * @description:
     * @author zhouyy
     * @date: 2022/6/25 16:08
     */
    private static List<String> getFileList() {
        List list = new ArrayList();
        //路径里必须包含 /src/工程包名称

        return list;
    }

    public static void main(String[] args)  throws Exception{
        List<String> fileList = getFileList();
        copyFile(convertFile(fileList));
        copyJar();
        deleteJarDir();
        logIgnoreAndNotExist();
    }

    private static void logIgnoreAndNotExist() {
        System.out.println("忽略文件。");
        for (String s : ignoreList) {
            System.out.println("       "+s);
        }
        System.out.println("不存在文件。");
        for (String s : noExistsList) {
            System.out.println("       "+s);
        }
    }

    private static void deleteJarDir() {
        for (String moduleName : jarPathMap.keySet()) {
            String jarPath = jarPathMap.get(moduleName);
            File file = new File(jarPath.substring(0,jarPath.lastIndexOf(".")));
            System.out.println("要删除的文件夹路径="+file.getAbsolutePath());
            if(file.isDirectory() && file.exists()){
                file.delete();
                System.out.println("删除成功!");
            }
        }
    }


    private static void copyJar() throws Exception{
        for (String moduleName : jarPathMap.keySet()) {
            String jarPath = jarPathMap.get(moduleName);
            File file = new File(exportBasePath+File.separator+jarPath.substring(jarPath.lastIndexOf(File.separator)+1));
            System.out.println("原jar包路径="+jarPath);
            System.out.println("目标jar包路径="+file.getAbsolutePath());
            copyFile(new File(jarPath), file);
        }
    }


    private static List<String> convertFile(List list) {
        List retrunList = new ArrayList();

        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            // /壹玖壹玖三期改造/系统实现/代码/T3.4.2021122101.Beta-version.47432/src/bt-biz-extension/src/main/java/com/bytter/ext/facade/sys/constant/ResourceExtConstant.java
            String file = (String) iterator.next();
            if(file.lastIndexOf(".iml")>0 || file.lastIndexOf(".sql") >0 || file.lastIndexOf(".SQL") >0 || file.lastIndexOf(".properties")>0){
                ignoreList.add(file);
                continue;
            }

            file = file.replaceAll("\\\\", "/");
            file = file.trim().replaceAll(" ","");
            // bt-biz-extension/src/main/java/com/bytter/ext/facade/sys/constant/ResourceExtConstant.java
            file = file.substring(file.indexOf("/"+src +"/")+src.length()+2);
            //bt-biz-extension
            String moduleName = file.substring(0,file.indexOf("/"));
            String jarPath = projectPath + File.separator+moduleName+File.separator+"target"+File.separator+moduleName+"-0.1-SNAPSHOT.jar";
            String dirPath = jarPath.substring(0,jarPath.lastIndexOf("."))+File.separator;
            if(!moduleName.equals(btWeb) ){
                jarPathMap.put(moduleName,jarPath);
                if((!jarUnZipMap.containsKey(moduleName) || !jarUnZipMap.get(moduleName))){
                    unZipJar(jarPath);
                    jarUnZipMap.put(moduleName,true);
                }
                //java 文件
                if(file.lastIndexOf(".java") >0){
                    file = dirPath+file.substring(file.indexOf("/com/")+1);
                    file = file.substring(0,file.lastIndexOf(".java"))+".class";
                }else{
                    file = dirPath+file.substring(file.indexOf("/resources/")+"/resources/".length());
                }
            }else{
                file = dirPath + file.substring(file.indexOf("/webapp/")+"/webapp/".length());
            }
            System.out.println("新的文件路径:"+file);
            retrunList.add(file);
        }
        return retrunList;
    }


    /**
     * @description:
     * @author zhouyy
     * @date: 2022/6/25 16:55
     */
    private static void unZipJar(String jarPath) {
        //E:\workspace-2021\T2\chengdu-jianguoqiche\T2\BTFP_V3.4_20200713\src\bt-biz-extension\target\bt-biz-extension-0.1-SNAPSHOT
        String dirPath = jarPath.substring(0,jarPath.lastIndexOf("."))+File.separator;
        File file = new File(dirPath);
        if(file.exists()){
            file.delete();
        }
        ZipUtil.unZip(jarPath,dirPath);

    }


    private static void copyFile(List<String> list) throws Exception {
        System.out.println("待复制文件数量为:【"+list.size()+"】");
        int trueCount = 0;
        for (int i = 0; i < list.size(); i++) {
            String sourceFilePath = (String) list.get(i);
            sourceFilePath = sourceFilePath.replaceAll("\\\\","/");
            String sourceDirPath = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/"));
            String sourceFileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/")+1);

            System.out.println("-----开始拷贝文件:"+sourceFileName);

            String targetFilePath = exportBasePath + "/"+sourceFilePath.substring(sourceFilePath.indexOf("/target/")+"/target/".length());
            File sourceFile = new File(sourceFilePath);
            System.out.println("源路径:"+sourceFile.getAbsolutePath());
            if(sourceFile.isDirectory()){
                System.out.println("文件夹。next ");
                continue;
            }
            if(!sourceFile.exists()){
                System.out.println("文件不存在。next ");
                noExistsList.add(sourceFile.getAbsolutePath());
                continue;
            }
            String targetDir = targetFilePath.substring(0,targetFilePath.lastIndexOf("/"));
            if(!new File(targetDir).exists()){
                new File(targetDir).mkdirs();
            }

            File[] files = new File(sourceDirPath).listFiles();
            for (int j = 0; j < files.length; j++) {
                if(files[j].isDirectory()) {
                    continue;
                }
                String fileName = files[j].getName();
                //BUG
                boolean flag1 = fileName.indexOf(sourceFileName.substring(0, sourceFileName.lastIndexOf(".")+1)) == 0;
                boolean flag2 = fileName.contains(sourceFileName.substring(0, sourceFileName.lastIndexOf("."))+"$") && fileName.endsWith(".class");
                if(flag1 || flag2) {
                    //开始复制文件
                    File fileSource = new File(sourceDirPath + File.separator + fileName);
                    String trueTargetFilePath = targetFilePath.replaceAll("\\\\","/");
                    if(flag2){
                        trueTargetFilePath = targetFilePath.substring(0,targetFilePath.lastIndexOf("/")+1)+fileName;
                    }
                    // 要导出的文件路径
                    File targetFile = new File(trueTargetFilePath);
                    System.out.println("目标路径:"+targetFile.getAbsolutePath());
                    copyFile(fileSource, targetFile);
                    System.out.println(fileName);
                    trueCount++;
                }
            }
            System.out.println("-----完成拷贝文件:"+sourceFileName);
        }
        System.out.println("-----文件数量:"+trueCount);
        System.out.println("补丁在磁盘中的路径:\n"+ exportBasePath);

    }

    // 复制文件
    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        // 新建文件输入流并对它进行缓冲
        FileInputStream input = new FileInputStream(sourceFile);
        BufferedInputStream inBuff = new BufferedInputStream(input);

        // 新建文件输出流并对它进行缓冲
        FileOutputStream output = new FileOutputStream(targetFile);
        BufferedOutputStream outBuff = new BufferedOutputStream(output);

        // 缓冲数组
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = inBuff.read(b)) != -1) {
            outBuff.write(b, 0, len);
        }
        // 刷新此缓冲的输出流
        outBuff.flush();

        // 关闭流
        inBuff.close();
        outBuff.close();
        output.close();
        input.close();
    }

    class FileVo {
        private String filePath;
        private String fileName;

        public FileVo() {
            super();
        }

        public FileVo(String filePath, String fileName) {
            super();
            this.filePath = filePath;
            this.fileName = fileName;
        }

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

    }

}

 

package com.bytter.ext.test;

import com.bytter.core.utils.ZipUtil;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class CopyT2ClassAndResouce {

//项目地址
static String src="src_50764";
static String projectPath=("D:\\software\\workspace\\t2\\fujian_haokelai\\code\\"+src).replaceAll("\\\\", "/");
static String exportBasePath="E:\\bug\\T2-华中部\\福建-豪客来\\补丁".replaceAll("\\\\", "/")+ File.separator+new SimpleDateFormat("yyyyMMddHHmm").format(new Date());

static Map<String,String> jarPathMap = new HashMap<>();
static Map<String,Boolean> jarUnZipMap = new HashMap<>();
static String extensionName = "bt-biz-extension";
static String btWeb = "bt-web";
static List<String> ignoreList = new ArrayList<>();
static List<String> noExistsList = new ArrayList<>();



/**
* @description:
* @author zhouyy
* @date: 2022/6/25 16:08
*/
private static List<String> getFileList() {
List list = new ArrayList();
//路径里必须包含 /src/工程包名称

return list;
}

public static void main(String[] args) throws Exception{
List<String> fileList = getFileList();
copyFile(convertFile(fileList));
copyJar();
deleteJarDir();
logIgnoreAndNotExist();
}

private static void logIgnoreAndNotExist() {
System.out.println("忽略文件。");
for (String s : ignoreList) {
System.out.println(" "+s);
}
System.out.println("不存在文件。");
for (String s : noExistsList) {
System.out.println(" "+s);
}
}

private static void deleteJarDir() {
for (String moduleName : jarPathMap.keySet()) {
String jarPath = jarPathMap.get(moduleName);
File file = new File(jarPath.substring(0,jarPath.lastIndexOf(".")));
System.out.println("要删除的文件夹路径="+file.getAbsolutePath());
if(file.isDirectory() && file.exists()){
file.delete();
System.out.println("删除成功!");
}
}
}


private static void copyJar() throws Exception{
for (String moduleName : jarPathMap.keySet()) {
String jarPath = jarPathMap.get(moduleName);
File file = new File(exportBasePath+File.separator+jarPath.substring(jarPath.lastIndexOf(File.separator)+1));
System.out.println("jar包路径="+jarPath);
System.out.println("目标jar包路径="+file.getAbsolutePath());
copyFile(new File(jarPath), file);
}
}


private static List<String> convertFile(List list) {
List retrunList = new ArrayList();

for (Iterator iterator = list.iterator(); iterator.hasNext();) {
// /壹玖壹玖三期改造/系统实现/代码/T3.4.2021122101.Beta-version.47432/src/bt-biz-extension/src/main/java/com/bytter/ext/facade/sys/constant/ResourceExtConstant.java
String file = (String) iterator.next();
if(file.lastIndexOf(".iml")>0 || file.lastIndexOf(".sql") >0 || file.lastIndexOf(".SQL") >0 || file.lastIndexOf(".properties")>0){
ignoreList.add(file);
continue;
}

file = file.replaceAll("\\\\", "/");
file = file.trim().replaceAll("","");
// bt-biz-extension/src/main/java/com/bytter/ext/facade/sys/constant/ResourceExtConstant.java
file = file.substring(file.indexOf("/"+src +"/")+src.length()+2);
//bt-biz-extension
String moduleName = file.substring(0,file.indexOf("/"));
String jarPath = projectPath + File.separator+moduleName+File.separator+"target"+File.separator+moduleName+"-0.1-SNAPSHOT.jar";
String dirPath = jarPath.substring(0,jarPath.lastIndexOf("."))+File.separator;
if(!moduleName.equals(btWeb) ){
jarPathMap.put(moduleName,jarPath);
if((!jarUnZipMap.containsKey(moduleName) || !jarUnZipMap.get(moduleName))){
unZipJar(jarPath);
jarUnZipMap.put(moduleName,true);
}
//java 文件
if(file.lastIndexOf(".java") >0){
file = dirPath+file.substring(file.indexOf("/com/")+1);
file = file.substring(0,file.lastIndexOf(".java"))+".class";
}else{
file = dirPath+file.substring(file.indexOf("/resources/")+"/resources/".length());
}
}else{
file = dirPath + file.substring(file.indexOf("/webapp/")+"/webapp/".length());
}
System.out.println("新的文件路径:"+file);
retrunList.add(file);
}
return retrunList;
}


/**
* @description:
* @author zhouyy
* @date: 2022/6/25 16:55
*/
private static void unZipJar(String jarPath) {
//E:\workspace-2021\T2\chengdu-jianguoqiche\T2\BTFP_V3.4_20200713\src\bt-biz-extension\target\bt-biz-extension-0.1-SNAPSHOT
String dirPath = jarPath.substring(0,jarPath.lastIndexOf("."))+File.separator;
File file = new File(dirPath);
if(file.exists()){
file.delete();
}
ZipUtil.unZip(jarPath,dirPath);

}


private static void copyFile(List<String> list) throws Exception {
System.out.println("待复制文件数量为:【"+list.size()+"");
int trueCount = 0;
for (int i = 0; i < list.size(); i++) {
String sourceFilePath = (String) list.get(i);
sourceFilePath = sourceFilePath.replaceAll("\\\\","/");
String sourceDirPath = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/"));
String sourceFileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/")+1);

System.out.println("-----开始拷贝文件:"+sourceFileName);

String targetFilePath = exportBasePath + "/"+sourceFilePath.substring(sourceFilePath.indexOf("/target/")+"/target/".length());
File sourceFile = new File(sourceFilePath);
System.out.println("源路径:"+sourceFile.getAbsolutePath());
if(sourceFile.isDirectory()){
System.out.println("文件夹。next ");
continue;
}
if(!sourceFile.exists()){
System.out.println("文件不存在。next ");
noExistsList.add(sourceFile.getAbsolutePath());
continue;
}
String targetDir = targetFilePath.substring(0,targetFilePath.lastIndexOf("/"));
if(!new File(targetDir).exists()){
new File(targetDir).mkdirs();
}

File[] files = new File(sourceDirPath).listFiles();
for (int j = 0; j < files.length; j++) {
if(files[j].isDirectory()) {
continue;
}
String fileName = files[j].getName();
//BUG
boolean flag1 = fileName.indexOf(sourceFileName.substring(0, sourceFileName.lastIndexOf(".")+1)) == 0;
boolean flag2 = fileName.contains(sourceFileName.substring(0, sourceFileName.lastIndexOf("."))+"$") && fileName.endsWith(".class");
if(flag1 || flag2) {
//开始复制文件
File fileSource = new File(sourceDirPath + File.separator + fileName);
String trueTargetFilePath = targetFilePath.replaceAll("\\\\","/");
if(flag2){
trueTargetFilePath = targetFilePath.substring(0,targetFilePath.lastIndexOf("/")+1)+fileName;
}
// 要导出的文件路径
File targetFile = new File(trueTargetFilePath);
System.out.println("目标路径:"+targetFile.getAbsolutePath());
copyFile(fileSource, targetFile);
System.out.println(fileName);
trueCount++;
}
}
System.out.println("-----完成拷贝文件:"+sourceFileName);
}
System.out.println("-----文件数量:"+trueCount);
System.out.println("补丁在磁盘中的路径:\n"+ exportBasePath);

}

// 复制文件
public static void copyFile(File sourceFile, File targetFile) throws IOException {
// 新建文件输入流并对它进行缓冲
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff = new BufferedInputStream(input);

// 新建文件输出流并对它进行缓冲
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff = new BufferedOutputStream(output);

// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();

// 关闭流
inBuff.close();
outBuff.close();
output.close();
input.close();
}

class FileVo {
private String filePath;
private String fileName;

public FileVo() {
super();
}

public FileVo(String filePath, String fileName) {
super();
this.filePath = filePath;
this.fileName = fileName;
}

public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

}

}
posted @ 2022-09-20 17:33  _万古如长夜  阅读(55)  评论(0编辑  收藏  举报