`
uule
  • 浏览: 6323867 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

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);
	}

}

 

。。。

 

 

分享到:
评论

相关推荐

    java代码调用ant执行类

    public class AntExecutor { public static void main(String[] args) { try { Main.main(new String[]{"-f", "path/to/build.xml", "targetName"}); } catch (BuildException e) { e.printStackTrace(); } }...

    Java调用Ant API用法收集

    public class AntExecutor { public static void main(String[] args) { Project project = new Project(); project.init(); project.setUserProperty("ant.file", "path/to/build.xml"); // 指定构建文件路径 ...

    人工智能与伦理问题的介绍

    人工智能与伦理问题的介绍

    scikit_learn-1.4.2-cp39-cp39-macosx_12_0_arm64.whl

    该资源为scikit_learn-1.4.2-cp39-cp39-macosx_12_0_arm64.whl,欢迎下载使用哦!

    英语单词词汇词根总表PDF

    学英语、背单词的好工具

    MongoDB在Linux环境下的安装、基本操作、可视化工具及实验源码与报告.docx

    MongoDB是一个高性能、开源、无模式的文档型数据库,适用于大规模数据存储和实时数据查询。在Linux环境下安装MongoDB,首先需要确定你的Linux发行版(如CentOS、Ubuntu等),并检查系统的硬件和软件要求是否满足MongoDB的安装需求。

    神经网络教程&案例&相关项目.docx

    撰写一篇关于神经网络的教程、案例及相关项目的5000字文章是一个庞大的任务,但我可以为您提供一个结构化的概览,并涵盖关键内容。以下是一个简化的版本,旨在概述神经网络的基础知识、一些经典案例以及相关的项目实践。 神经网络教程 引言 神经网络(Neural Networks, NNs)是模仿生物神经系统结构和功能的一种计算模型。它们由大量的人工神经元(也称为节点)组成,这些神经元通过可调的权重相互连接,能够处理复杂的信息并做出决策。随着深度学习技术的发展,神经网络在图像识别、自然语言处理、语音识别等领域取得了显著成就。 基础概念 神经元:神经网络的基本单元,模拟生物神经元的结构和功能。每个神经元接收来自其他神经元的输入信号,通过加权求和、激活函数处理后产生输出信号。 激活函数:用于引入非线性因素,使得神经网络能够处理非线性问题。常见的激活函数包括Sigmoid、ReLU、Tanh等。 层:神经网络中的神经元按层组织,通常包括输入层、隐藏层(一个或多个)和输出层。输入层接收原始数据,隐藏层进行特征提取和转换,输出层产生最终结果。 前向传播:输入信号从输入层经过隐藏层到达输出层的过程,计算

    电网公司数字化转型规划与实践两个材料.pptx

    电网公司数字化转型规划与实践两个材料.pptx

    Python项目开发实战:看图猜成语小程序(案例教程实例课程).pdf

    Python项目开发实战:看图猜成语小程序(案例教程实例课程).pdf

    制造业资产管理数字化顶层设计方案[19页PPT].pptx

    制造业资产管理数字化顶层设计方案[19页PPT]

    集团制造企业数字化转型顶层业务设计方案.pptx

    集团制造企业数字化转型顶层业务设计方案.pptx

    1.6-1.7(学生上传).PPT

    1.6-1.7(学生上传).PPT

    Java面试试题(二).docx

    2024java面试题 通过对基础知识、面向对象编程、高级主题、分布式系统、微服务架构、安全性和实践问题的深入解析,帮助你在Java面试中脱颖而出,成功拿到理想的Offer。

    scikit_learn-1.4.2-cp312-cp312-macosx_10_9_x86_64.whl

    该资源为scikit_learn-1.4.2-cp312-cp312-macosx_10_9_x86_64.whl,欢迎下载使用哦!

    电子科技大学计算机组成原理实验课1-实验4:中小规模时序逻辑设计

    电子科技大学计算机组成原理实验课1——实验4:中小规模时序逻辑设计

    第13章 带传动与链传动-西科大.ppt

    第13章 带传动与链传动-西科大

    “python-研究生录取概率统计分析A”文章对应的数据文件

    “python-研究生录取概率统计分析A”文章对应的数据文件

    大型企业数字化转型管控平台解决方案两个文档.pptx

    大型企业数字化转型管控平台解决方案两个文档.pptx

    毕业设计的案例说明.txt毕业设计的案例说明.txt

    毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案例说明.txt毕业设计的案

    springboot vue前后端分离.docx

    在实现 Spring Boot 后端和 Vue.js 前端的分离开发时,通常会采用前后端分离的架构来提高开发效率和灵活性。以下是一般步骤和关键点: 后端(Spring Boot)开发步骤: 创建 Spring Boot 项目: 使用 Spring Initializr 或者 Maven/Gradle 手动创建一个 Spring Boot 项目。 编写后端业务逻辑: 创建控制器(Controller)、服务(Service)、数据访问对象(Repository/DAO)等。 定义 RESTful API 接口,处理前端请求并返回 JSON 数据。 配置跨域资源共享(CORS)(可选): 在 Spring Boot 应用中配置允许的跨域请求,以便前端可以安全地访问后端服务。 集成数据库访问: 使用 Spring Data JPA 或者 MyBatis 等持久层框架操作数据库。 安全性配置(可选): 集成 Spring Security 进行认证和授权,保护后端 API 的安全性。

Global site tag (gtag.js) - Google Analytics