AntExecutor 组装执行【本地命令】

1、组装生成ant 打包、发布命令

String[] cmd = AntExecutor.generateCmd(project.getLocation() + File.separator + "build.xml", "jar", null);
//String[] cmd = AntExecutor.generateCmd(project.getLocation() + File.separator + "build.xml", "deploy", null);
detail.append(Arrays.toString(cmd)).append("\n");
detail.append(AntExecutor.execute(cmd).replaceAll("\n", "\n    "));
public class AntExecutor {

	private static final Logger logger = LoggerFactory.getLogger(AntExecutor.class);
	
	/**
	 * @param cmd ant命令
	 * 
	 * @return 执行日志
	 * 
	 * @exception 执行有错误会抛出AntExecException
	 */
	public static String execute(String[] cmd){
		// 标准输出
		InputStream standardInput = null;
		BufferedReader standardReader = null;
		// 错误输出
		InputStream errInput = null;
		BufferedReader errReader = null;
		
		StringBuilder standardMsg = new StringBuilder();
		StringBuilder errMsg = new StringBuilder();
		
		Process process = null;
		try {
			// 执行ant
			process = Runtime.getRuntime().exec(cmd);
			// 读取标准输入流
			standardInput = process.getInputStream();
			if(standardInput != null){
				standardReader = new BufferedReader(new InputStreamReader(standardInput));
				String line = standardReader.readLine();
				while (null != line) {
					standardMsg.append(line).append("\n");
					line = standardReader.readLine();
				}
			}

			// 检查错误流是否有信息,如果有,则说明执行失败
			errInput = process.getErrorStream();
			if(errInput != null){
				errReader = new BufferedReader(new InputStreamReader(errInput));
				String errLine = errReader.readLine();
				while (null != errLine) {
					errMsg.append(errLine).append("\n");
					errLine = errReader.readLine();
				}
				if(errMsg.length() > 0){
					throw new AntExecException(standardMsg.append(errMsg).toString());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new AntExecException(standardMsg.append(errMsg).toString());
		} finally {
			// 销毁进程对象,否则可能引起资源不能释放问题,出现Too many open files错误
			if (process != null) {
				process.destroy();
			}
			if (null != standardReader) {
				try {
					standardReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != errReader) {
				try {
					standardReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		logger.debug("executeAnt end...");
		return standardMsg.toString();
	}
	
	/**
	 * 生成命令
	 * 
	 * @param buildFilePath
	 * @param task
	 * @return
	 */
	public static String[] generateCmd(String buildFilePath, String task, String[] parameter){
		String ant = "ant.bat";
		if (!System.getProperty("os.name").contains("Windows")) {
			ant = "ant";
		}
		String[] cmd = null;
		if(parameter != null){
			cmd = new String[4 + parameter.length];
			System.arraycopy(parameter, 0, cmd, 4, parameter.length);
		}else{
			cmd = new String[4];
		}
		cmd[0] = ant;
		cmd[1] = "-f";
		cmd[2] = buildFilePath;
		cmd[3] = task;
		return cmd;
	}
	
}
public class AntExecException extends RuntimeException {

	private static final long serialVersionUID = -3708533380275512493L;

	public AntExecException() {
		super();
	}

	public AntExecException(String message, Throwable cause) {
		super(message, cause);
	}

	public AntExecException(String message) {
		super(message);
	}

	public AntExecException(Throwable cause) {
		super(cause);
	}

}

。。。

猜你喜欢

转载自uule.iteye.com/blog/2147069