栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号