安装
- 官网下载压缩包,解压
- 设置环境变量
- 默认在当前目录下寻找build.xml文件来进行构建和执行
- ant -f helloworld.xml 可以带参数来指定要执行的文件
- build.xml文件要放在项目的根目录下,和src并列
- 自动处理路径中的正反斜杠的问题
- Ant还包含了CVS版本控制的标签,没有添加,等找到git的在补全吧
实例
创建文件夹
<?xml version="1.0" encoding="UTF-8"?>
<project default="init" name="Project">
<target name="init">
<mkdir dir="helloworld\a" />
<mkdir dir="hello" />
</target>
</project>
删除目录,可以是非空目录
<?xml version="1.0" encoding="UTF-8"?>
<project default="init">
<target name="init">
<delete dir="helloworld" />
</target>
</project>
执行顺序 属性 修饰description
- target的位置不重要,是靠depends属性来决定先后顺序的
<?xml version="1.0" encoding="UTF-8"?>
<project default="second">
<description>
hello world
</description>
<property name="hello" value="welcome" />
<target name="init" description="helloworld">
<mkdir dir="helloworld\a" />
<mkdir dir="hello" />
<mkdir dir="${hello}" />
</target>
<target name="second" depends="init">
<mkdir dir="abc" />
<mkdir dir="xyz" />
</target>
</project>
- 如果用ant -f helloworld.xml second 就不会再去执行init了
- ant init second 会按参数的顺序去执行,但second又是依赖于init的,所以init会显示执行了两次
执行顺序二
<?xml version="1.0" encoding="UTF-8"?>
<project default="package" basedir=".">
<target name="init" />
<target name="preprocess" depends="init" />
<target name="compile" depends="init,preprocess" />
<target name="package" depends="compile" />
</project>
综合
<?xml version="1.0" encoding="UTF-8"?>
<project default="package">
<property name="compile" value="compile" />
<property name="dist" value="dist" />
<property name="src" value="src" />
<target name="init" />
<target name="preprocess" depends="init">
<mkdir dir="${compile}" />
<mkdir dir="${dist}" />
</target>
<target name="compile" depends="init,preprocess" />
<target name="package" depends="compile" />
<target name="MyCompile" depends="preprocess">
<javac srcdir="src" destdir="${compile}">
</javac>
</target>
<target name="dist" depends="MyCompile">
<tstamp></tstamp>
<jar destfile="${dist}/package-${DSTAMP}.jar" basedir="${compile}">
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Main-Class" value="com.shengsiyuan.image.google.Test" />
</manifest>
</jar>
</target>
<target name="deleteFile">
<delete file="${dist}/packagexxx.jar"></delete>
<delete dir="${dist}"></delete>
</target>
<target name="copyFile">
<copy file="src/test.java" tofile="c:/test2.java"></copy>
<copy file="src/test.java" todir="c:/"></copy>
</target>
<target name="moveFile">
<move file="src/test.java" todir="c:/"></move>
</target>
<target name="compress" depends="MyCompile">
<zip destfile="${dist}" basedir="${compile}"></zip>
</target>
<target name="uncompress" depends="compress">
<unzip dest="${dist}" src="${dist}/package.zip"></unzip>
</target>
<target name="replaceOperation">
<replace file="input.txt" token="how" value="what" summary="on"></replace>
</target>
<target name="copy2">
<copy todir="${dist}">
<fileset dir="src">
<include name="**/*.java" />
</fileset>
</copy>
</target>
<taskdef name="myFileSorterExample" classname="com.shengsiyuan.ant.tools.extend.FileSorter" classpath="bin"></taskdef>
<target name="myFileSorter">
<myFileSorterExample srcFile="input.txt" destFile="output.txt" />
</target>
</project>