Ant与批处理(win环境)学习笔记
背景:最近个人附带的负责维护测试服务环境,每天早上的过程大致如下:
远程登录到主机438——进入构建目录——删除Cloud和dist两个文件夹——进入ant目录——键入build开始构建(由于系统很大一般要12分钟)——构建完成之后打开SSH File Transfer将新构建的连个文件夹上传到Rhel服务器——在Rhel中启动cloud和dc。
刚开始还好,搞得天数多了难免不生厌,于是想如何使上述这一过程做到最大程度的自动化呢?
想到的一种实现方式:
a、将build做成随机启动(因服务器一般都不关闭的,因此考虑做成定时的)(写.cmd脚本)
b、将删除与上传工的作放进构建脚本中(修改ant脚本)
于是开始行动, 先去系统地学习一下ant的使用... ...
————————Ant学习之简单Demo——————
一、基本概念
1、什么是Ant
2、什么是构建
3、使用Ant的优势(相对于make而言)
.跨平台
.使用简单
.语法清晰
.功能强大
二、Ant安装
1、http://ant.apache.org/bindownload.cgi下载Ant,当前最新版本为1.8.1
2、 与配置Jdk的环境类似,新建一个变量:ANT_HOME=ant所在目录
设置Path为: %ANT_HOME%\bin
注意:如果要让Ant能够支持Junit,需要将JUnit的junit.jar放置在Ant的lib目录并记得修改ClassPath中原有关Junit的设定。
3、 进入CMD界面,键入ant -v
若出现有关版本的提示则表明你的安装成功了。
好了,让我们开始进入Ant的编写... ...
三、Ant简单Demo
目的: 使用Ant来达成目的,完成一件事情的实例
.编写一个程序
.编译它们
.把它们打成jar包
.设定它们的存放位置
.运行它们
1、首先写一个HelloWorld.java
package test.ant;public class HelloWorld {public static void main(String[] args) {System.out.println("Welcome to The Ant World");}}
<?xml version="1.0" encoding="UTF-8"?><project name="HelloWorld" default="run" basedir="."> <property name="src" value="src"/> <property name="dest" value="classes"/> <property name="hello_jar" value="hello1.jar"/> <target name="init"> <mkdir dir="${dest}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${dest}" includeantruntime="false"/> </target> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <java classname="test.ant.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="return" depends="clean,run"> <ant target="clean"/> <ant target="run"/> </target></project>
<?xml version="1.0" encoding="UTF-8"?><project name="main" default="build" basedir="."> <property name="bin" value="${basedir}\bin"/> <property name="src1" value="${basedir}\src1"/> <property name="src2" value="${basedir}\src2"/> <property name="src3" value="${basedir}\src3"/> <target name="init"> <mkdir dir="${bin}"/> </target> <target name="run"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="clean"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="build" depends="init"> <copy todir="${bin}"> <fileset dir="${src1}"> <include name="*.jar"/> </fileset> <fileset dir="${src2}"> <include name="*.jar"/> </fileset> <fileset dir="${src3}"> <include name="*.jar"/> </fileset> </copy> </target> <target name="rebuild" depends="build,clean"> <ant target="clean"/> <ant target="build"/> </target></project>OK,你的任务完成了!
<?xml version="1.0" encoding="UTF-8"?><project name="main" default="build" basedir="."> <property name="bin" value="${basedir}\bin"/> <property file="all.properties"/> <target name="init"> <mkdir dir="${bin}"/> </target> <target name="run"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="clean"> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="build" depends="init"> <copy todir="${bin}"> <fileset dir="${src1}"> <include name="*.jar"/> </fileset> <fileset dir="${src2}"> <include name="*.jar"/> </fileset> <fileset dir="${src3}"> <include name="*.jar"/> </fileset> </copy> </target> <target name="rebuild" depends="build,clean"> <ant target="clean"/> <ant target="build"/> </target></project>
<?xml version="1.0" encoding="UTF-8"?> <property name="src" value="src"/> <property name="dest" value="classes"/> <target name="test"> <ant target="run"/> </target></xml>
<!-- include a xml file, it can be common property, can be also a target --> <!DOCTYPE project[ <!ENTITY share-variable SYSTEM "file:../include.xml"> ]>然后再在build.xml中的<project></project>之间写入 &share-variable;即可
<?xml version="1.0" encoding="UTF-8"?> <!-- include a xml file, it can be common property, can be also a target --> <!DOCTYPE project[ <!ENTITY share-variable SYSTEM "file:../include.xml"> ]><project name="HelloWorld" default="run" basedir="."><!--use the include -->&share-variable;<!--define the property --><!-- via include <property name="src" value="src"/> <property name="dest" value="classes"/>--> <property name="hello_jar" value="hello1.jar"/><!--define op --> <target name="init"> <mkdir dir="${dest}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${dest}" includeantruntime="false"/> </target> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <java classname="test.ant.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="return" depends="clean,run"> <ant target="clean"/> <ant target="run"/> </target></project>