java使用cmd命令 *** 作maven批量打包(并复制jar包到一个文件分类)

java使用cmd命令 *** 作maven批量打包(并复制jar包到一个文件分类),第1张

java使用cmd命令 *** 作maven批量打包(并复制jar包到一个文件分类)

一、java类

package com.lmp.generator;

import com.lmp.generator.common.utils.DateUtils;
import lombok.SneakyThrows;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


public class PackageHscMhs {
    
    private static final String PACKAGE_SOURCE_PATH = "D:\lmp\hwasee\source\health_mhs_package";
    private static final List packageCmds = Arrays.asList(
            "cmd /k  cd D:\lmp\hwasee\source\health_mhs_package\hwasee-health-mhs && mvn clean package -Dmaven.test.skip=true",
            "cmd /k  cd D:\lmp\hwasee\source\health_mhs_package\hwasee-health-hsc && mvn clean package -Dmaven.test.skip=true",
            "cmd /k  cd D:\lmp\hwasee\source\health_mhs_package\hwasee-health-hdc && mvn clean package -Dmaven.test.skip=true",
            "cmd /k  cd D:\lmp\hwasee\source\health_mhs_package\hwasee-health-fs && mvn clean package -Dmaven.test.skip=true"
    );

    
    private static final String PACKAGE_TARGET_PATH = "C:\Users\HUAWEI\Desktop\mhs打包";
    
    private static final int JAR_NUMBER = 18;
    
    private static final int LAST_TIME = 1000 * 60 * 10;

    @SneakyThrows
    public static void main(String[] args) {
        //异步打包
        for(String cmd : packageCmds){
            Thread t = new PackageCmdThread(cmd);
            t.start();
        }
        String targetPath = createPathAdd(PACKAGE_TARGET_PATH + "\" + DateUtils.getDate());
        int okJarNumber = 0;
        while (true) {
            List jarPaths = new ArrayList<>();
            traverseFolder(jarPaths, PACKAGE_SOURCE_PATH);
            for (String jarPath : jarPaths) {
                File jar = new File(jarPath);
                String type = "";
                if (jar.getName().indexOf("hsc") > -1) {
                    type = "hsc";
                } else if (jar.getName().indexOf("mhs") > -1) {
                    type = "mhs";
                } else {
                    type = "ufs";
                }
                String okJarPath = createPath(targetPath + "\" + type) + "\" + jar.getName();
                File okJar = new File(okJarPath);
                if (!okJar.exists()) {
                    okJarNumber++;
                    System.out.println("文件:" + okJar.getAbsolutePath());
                    copyFileUsingFileChannels(jar, okJar);
                }

            }
            if (jarPaths.size() >= JAR_NUMBER) {
                break;
            } else {
                System.err.println("总计需要jar:" + JAR_NUMBER + ",已完成:" + okJarNumber + ",还差" + (JAR_NUMBER - okJarNumber));
                Thread.sleep(10000);
            }
        }
        System.err.println("----------------------------------打包完毕----------------------------------");
        System.err.println("路径:" + targetPath);
        System.err.println("----------------------------------打包完毕----------------------------------");
    }

    //判断是否已经复制
    private static boolean existsFile(File[] okJars, File jar) {
        for (File okJar : okJars) {
            if (okJar.getName().equals(jar.getName())) {
                return true;
            }
        }
        return false;
    }

    
    private static String createPathAdd(String path) {
        File file = new File(path);
        if (file.exists()) {
            int num = 1;
            String newPath = path + "_" + num;
            while (true) {
                file = new File(newPath);
                if (file.exists()) {
                    num++;
                    newPath = path + "_" + num;
                } else {
                    break;
                }
            }
            file.mkdir();
            path = newPath;
        } else {
            file.mkdir();
        }
        return path;
    }

    
    private static String createPath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        return path;
    }

    
    private static void traverseFolder(List paths, String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        traverseFolder(paths, file2.getAbsolutePath());
                    } else {
                        if (file2.getAbsolutePath().endsWith(".jar") && !file2.getAbsolutePath().endsWith("SNAPSHOT.jar") && !file2.getAbsolutePath().endsWith("sources.jar")) {
                            long fileTime = file2.lastModified();
                            long nowTime = new Date().getTime();
                            if ((nowTime - fileTime) <= LAST_TIME) {
                                paths.add(file2.getAbsolutePath());
                            }
                        }
                    }
                }
            }
        }
    }

    
    private static void copyFileUsingFileChannels(File source, File dest)
            throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }
}
//使用cmd命令操作maven打包(多线程执行)
class PackageCmdThread extends Thread {
    private String cmd;

    public PackageCmdThread(String cmd) {
        this.cmd = cmd;
    }

    @SneakyThrows
    @Override
    public void run() {
        Runtime run = Runtime.getRuntime();
        Process p = run.exec(cmd);
        InputStream inputStream = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "gb2312"));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

二、cmd打包命令

cmd /k cd D:lmphwaseesourcehealth_mhs_packagehwasee-health-mhs && mvn clean package -Dmaven.test.skip=true

cmd /k
cmd /c :是执行完dir命令后关闭命令窗口;
cmd /k :是执行完dir命令后不关闭命令窗口。

D:lmphwaseesourcehealth_mhs_packagehwasee-health-mhs && mvn clean package -Dmaven.test.skip=true
多个命令使用&&连接

三、多线程执行cmd命令代码

    public static void main(String[] args) {
        //异步打包
        for(String cmd : packageCmds){
            Thread t = new PackageCmdThread(cmd);
            t.start();
        }
    }
//使用cmd命令 *** 作maven打包(多线程执行)
class PackageCmdThread extends Thread {
    private String cmd;

    public PackageCmdThread(String cmd) {
        this.cmd = cmd;
    }

    @SneakyThrows
    @Override
    public void run() {
        Runtime run = Runtime.getRuntime();
        Process p = run.exec(cmd);
        InputStream inputStream = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "gb2312"));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5434540.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存