java调用执行ant命令

Runtime调用

public class TestInvokeDosCommand {
    private static Log log = LogFactory.getLog(TestInvokeDosCommand.class);
    public static void main(String[] args) {
        String antfile = "D:/公司相关/服务器更新/2016/HD";
        String cmd = "./build2.bat";
        try {
            //成功!直接执行ant
            //Process process = Runtime.getRuntime().exec("cmd /c ant",null,new File(antfile));
            //成功!执行bat命令:@echo off;call ant;
            //Process process = Runtime.getRuntime().exec(antfile+"/build3.bat",null,new File(antfile));
            //失败(一直在挂起,因为有pause命令)!执行bat命令:@echo off;call ant;pause
            //Process process = Runtime.getRuntime().exec(antfile+"/build.bat",null,new File(antfile));
            //成功!执行bat命令:mkdir aaa
            //Process process = Runtime.getRuntime().exec(antfile+"/build2.bat",null,new File(antfile));

            InputStream inputStream = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(inputStream);
            InputStream errorStream = process.getErrorStream();
            InputStreamReader esr = new InputStreamReader(errorStream);

            int n1;
            char[] c1 = new char[1024];
            StringBuffer standardOutput = new StringBuffer();
            while ((n1 = isr.read(c1)) > 0) {
                standardOutput.append(c1, 0, n1);
            }
            System.out.println("Standard Output: " + standardOutput.toString());

            int n2;
            char[] c2 = new char[1024];
            StringBuffer standardError = new StringBuffer();
            while ((n2 = esr.read(c2)) > 0) {
                standardError.append(c2, 0, n2);
            }
            System.out.println("Standard Error: " + standardError.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ProcessBuilder调用

你可能感兴趣的:(部署-自动化脚本)