1.1 添加环境变量:ANT_HOME=D:softwareantapache-ant-1.10.1
在path中添加:%ANT_HOME%in
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 default="run" basedir="." !-- 定义变量 -- property / property / property / !-- 定义target -- target !-- 创建目录 -- mkdir dir="$${dest}"/ /target !-- depends:依赖的target -- target depends="init" !-- 将srcdir目录中的文件进行编译,并将编译后的文件放入到destdir目录中 -- javac srcdir="$${src}" destdir="$${dest}"/ /target target depends="compile" !-- 将basedir目录中的文件打成jar包 -- jar jarfile="$${hello_jar}" basedir="$${dest}"/ /target target depends="build" !-- 运行classname -- java class classpath="$${hello_jar}"/ /target target !-- 删除dir目录和file文件 -- delete dir="$${dest}"/ delete file="$${hello_jar}"/ /target target 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 default="build" basedir="." property / property / property / property / target mkdir dir="$${bin}"/ /target target !-- 分别运行各个目录下的target -- ant dir="$${src1}" target="run"/ ant dir="$${src2}" target="run"/ ant dir="$${src3}" target="run"/ /target target ant dir="$${src1}" target="clean"/ ant dir="$${src2}" target="clean"/ ant dir="$${src3}" target="clean"/ /target target ant dir="$${src1}" target="build"/ ant dir="$${src2}" target="build"/ ant dir="$${src3}" target="build"/ /target target depends="init,call" !-- 复制指定的文件到todir -- copy todir="$${bin}" fileset dir="$${src1}" include / /fileset fileset dir="$${src2}" include / /fileset fileset dir="$${src3}" include / /fileset /copy /target target depends="build,clean" ant target="clean"/ ant target="build"/ /target /project 3 使用properties文件配置属性,和公共xml
3.1 创建all.properties设置变量
src1=D:\software\ant\test\test3\src1src2=D:\software\ant\test\test3\src2src3=D:\software\ant\test\test3\src3
3.2 创建include.xml,设置公共的变量和target
?xml version="1.0" encoding="UTF-8"? property / property / target ant target="run"/ /target
3.3 在总build中使用使用all.properties设置变量
?xml version="1.0" encoding="UTF-8"? project default="build" basedir="." !-- 读取配置文件中的变量 -- property file="all.properties"/ property / target mkdir dir="$${bin}"/ /target target ant dir="$${src1}" target="run"/ ant dir="$${src2}" target="run"/ ant dir="$${src3}" target="run"/ /target target ant dir="$${src1}" target="clean"/ ant dir="$${src2}" target="clean"/ ant dir="$${src3}" target="clean"/ /target target ant dir="$${src1}" target="build"/ ant dir="$${src2}" target="build"/ ant dir="$${src3}" target="build"/ /target target depends="init,call" copy todir="$${bin}" fileset dir="$${src1}" include / /fileset fileset dir="$${src2}" include / /fileset fileset dir="$${src3}" include / /fileset /copy /target target depends="build,clean" ant target="clean"/ ant target="build"/ /target target 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 default="run" basedir="." !-- 使用变量 -- share-variable; !-- 这两个变量在公用的xml中已经定义,使用 share-variable就可直接使用 property / property / property / target mkdir dir="$${dest}"/ /target target depends="init" javac srcdir="$${src}" destdir="$${dest}"/ /target target depends="compile" jar jarfile="$${hello_jar}" basedir="$${dest}"/ /target target depends="build" java class classpath="$${hello_jar}"/ /target target delete dir="$${dest}"/ delete file="$${hello_jar}"/ /target target depends="clean,run" ant target="clean"/ ant target="run"/ /target /project