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;

/**
 * HSC MHS打包
 */
public class PackageHscMhs {
    /**
     * 打包地址
     */
    private static final String PACKAGE_SOURCE_PATH = "D:\\lmp\\hwasee\\source\\health_mhs_package";
    private static final List<String> 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;
    /**
     * 10分钟内打的包
     */
    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<String> 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;
    }

    /**
     * 判断文件夹是否存在,不存在则创建,存在则加后缀_1,_2,_3...
     */
    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;
    }

    /**
     * 判断文件夹是否存在,不存在则创建,存在则加后缀_1,_2,_3...
     */
    private static String createPath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        return path;
    }

    /**
     * 遍历查找打包地址下所有jar包
     */
    private static void traverseFolder(List<String> 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());
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 复制文件
     *
     * @param source
     * @param dest
     * @throws IOException
     */
    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:\lmp\hwasee\source\health_mhs_package\hwasee-health-mhs && mvn clean package -Dmaven.test.skip=true

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

D:\lmp\hwasee\source\health_mhs_package\hwasee-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);
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值