标签:val variable 测试 ati def 3.4 pen 编译 delete
1.ant的安装
1.1 添加环境变量:ANT_HOME=D:\software\ant\apache-ant-1.10.1
在path中添加:%ANT_HOME%\bin
1.2 测试是否安装成功
在cmd中输入ant,如果出现如下提示表示安装成功
2.定义简单的build.xml
2.1 创建HelloWord.java
package test; public class HelloWorld{ public static void main(String[] args){ System.out.println("hello world!"); } }
2.2 创建build.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- 定义project节点,和默认运行的target,和工作根目录 --> <project name="HelloWorld" default="run" basedir="."> <!-- 定义变量 --> <property name="src" value="src"/> <property name="dest" value="classes"/> <property name="hello_jar" value="hello.jar"/> <!-- 定义target --> <target name="init"> <!-- 创建目录 --> <mkdir dir="${dest}"/> </target> <!-- depends:依赖的target --> <target name="compile" depends="init"> <!-- 将srcdir目录中的文件进行编译,并将编译后的文件放入到destdir目录中 --> <javac srcdir="${src}" destdir="${dest}"/> </target> <target name="build" depends="compile"> <!-- 将basedir目录中的文件打成jar包 --> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <!-- 运行classname --> <java classname="test.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <!-- 删除dir目录和file文件 --> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="rerun" depends="clean,run"> <!-- 运行clean和run target --> <ant target="clean"/> <ant target="run"/> </target> </project>
3. 合并多个build.xml
3.1 假设下边有三个小组,每个小组负责一部分,他们每个下面都有src和build.xml
3.2 创建一个总的build.xml
<?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"> <!-- 分别运行各个目录下的target --> <ant dir="${src1}" target="run"/> <ant dir="${src2}" target="run"/> <ant dir="${src3}" target="run"/> </target> <target name="clean"> <ant dir="${src1}" target="clean"/> <ant dir="${src2}" target="clean"/> <ant dir="${src3}" target="clean"/> </target> <target name="call"> <ant dir="${src1}" target="build"/> <ant dir="${src2}" target="build"/> <ant dir="${src3}" target="build"/> </target> <target name="build" depends="init,call"> <!-- 复制指定的文件到todir --> <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>
3 使用properties文件配置属性,和公共xml
3.1 创建all.properties设置变量
src1=D:\\software\\ant\\test\\test3\\src1
src2=D:\\software\\ant\\test\\test3\\src2
src3=D:\\software\\ant\\test\\test3\\src3
3.2 创建include.xml,设置公共的变量和target
<?xml version="1.0" encoding="UTF-8"?> <property name="src" value="src"/> <property name="dest" value="classes"/> <target name="test"> <ant target="run"/> </target>
3.3 在总build中使用使用all.properties设置变量
<?xml version="1.0" encoding="UTF-8"?> <project name="main" default="build" basedir="."> <!-- 读取配置文件中的变量 --> <property file="all.properties"/> <property name="bin" value="${basedir}\bin"/> <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="clean"/> <ant dir="${src2}" target="clean"/> <ant dir="${src3}" target="clean"/> </target> <target name="call"> <ant dir="${src1}" target="build"/> <ant dir="${src2}" target="build"/> <ant dir="${src3}" target="build"/> </target> <target name="build" depends="init,call"> <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> <target name="test"> <ant dir="${src1}" target="test"/> <ant dir="${src2}" target="test"/> <ant dir="${src3}" target="test"/> </target> </project>
3.4 在每个小组的build.xml中引用include.xml的变量和target
<?xml version="1.0" encoding="UTF-8" ?> <!-- 引入外部的xml,在本xml中就可以使用引入的xml中的变量和target --> <!DOCTYPE project[ <!ENTITY share-variable SYSTEM "file:../include.xml"> ]> <project name="HelloWorld" default="run" basedir="."> <!-- 使用变量 --> &share-variable; <!-- 这两个变量在公用的xml中已经定义,使用&share-variable就可直接使用 <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}"/> </target> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <java classname="test.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="rerun" depends="clean,run"> <ant target="clean"/> <ant target="run"/> </target> </project>
标签:val variable 测试 ati def 3.4 pen 编译 delete
原文地址:http://www.cnblogs.com/lifeone/p/7851785.html