1.下载解压ant安装包
2.配置环境变量PATH
3.Cmd下输入ant检测是否配置好
4.第一个例子
Build.xml
<?xml version=”1.0” encoding=”utf-8”?>
<project default=”execute”>
<target name=”compile”>
<javac destdir=”.” srcdir=”.”/>
</target>
<target name=”execute” depends=”compile”>
<java classpath=”.” classname=”HelloWorld”/>
</target>
</project>
5. 编译――>打包――>运行
(1)Build――>所有文件信息保存在这个文件中
Src――>所有源文件所在目录
Classes――>所有编译好的文件都在这个目录下
Dist――>编译好的jar文件在这个目录下
将src中的文件拷贝到build下的src目录下
(2)编译源代码
(3)将源代码打包成jar
(4)直接运行程序
01.xml
<?xml version="1.0" encoding="UTF-8"?>
<projectdefault="execute">
<!--文件集-->
<filesetdir="src"includes="**/*.java"></fileset>
<targetname="init">
<deletedir="build"></delete>
<mkdirdir="build"/>
<mkdirdir="build/src"/>
<mkdirdir="build/classes"/>
<mkdirdir="build/dist"/>
<!--<delete dir="build"></delete>-->
</target>
<targetname="copySrc"depends="init">
<copytodir="build/src">
<filesetdir="src"includes="**/*.java"></fileset>
</copy>
</target>
</project>
02.xml
<?xml version="1.0" encoding="UTF-8"?>
<projectdefault="execute">
<!--文件集-->
<filesetid="src.path"dir="src"includes="**/*.java"></fileset>
<filesetdir="src">
<includename="**/*.*"/>
<excludename="**/*.properties"/>
</fileset>
<targetname="init">
<deletedir="build"></delete>
<mkdirdir="build"/>
<mkdirdir="build/src"/>
<mkdirdir="build/classes"/>
<mkdirdir="build/dist"/>
<!--<delete dir="build"></delete>-->
</target>
<targetname="copySrc"depends="init">
<copytodir="build/src">
<filesetrefid="src.path"></fileset>
</copy>
</target>
</project>
03.xml
<?xml version="1.0" encoding="UTF-8"?>
<projectdefault="execute">
<!--文件集-->
<filesetid="src.path"dir="src"includes="**/*.java"></fileset>
<filesetdir="src">
<includename="**/*.*"/>
<excludename="**/*.properties"/>
</fileset>
<targetname="init">
<deletedir="build"></delete>
<mkdirdir="build"/>
<mkdirdir="build/src"/>
<mkdirdir="build/classes"/>
<mkdirdir="build/dist"/>
<!--<delete dir="build"></delete>-->
</target>
<targetname="copySrc"depends="init">
<copytodir="build/src">
<filesetrefid="src.path"></fileset>
</copy>
</target>
<targetname="compile"depends="init">
<javacdestdir="build/classes"srcdir="src"></javac>
</target>
<targetname="jar"depends="compile">
<jardestfile="build/dist/hello.jar"basedir="build/classes">
<manifest>
<!--Main-Class指明可以运行的文件-->
<attributename="Main-Class"value="com.zsj.app.HelloWorld"/>
<attributename="Build-By"value="zhangshijie"/>
</manifest>
</jar>
</target>
<targetname="execute"depends="jar,copySrc">
<echo>基于类路劲的classname来完成执行</echo>
<javaclassname="com.zsj.app.HelloWorld"classpath="build/classes">
<argvalue="zhangsan"/>
<argvalue="lisi"/>
<argvalue="wangwu"/>
</java>
<echo>基于jar文件执行,未设置fork的时候是ant的环境,设置为true之后用的是JDK的环境</echo>
<javajar="build/dist/hello.jar"fork="true">
<argvalue="zhangsan"/>
<argvalue="lisi"/>
<argvalue="wangwu"/>
</java>
</target>
</project>
04.xml
<?xml version="1.0" encoding="UTF-8"?>
<projectdefault="execute">
<!--可以将外部文件中的键值对因为build.xml的属性-->
<propertyfile="build.properties"></property>
<propertyname="build.dir"value="build"></property>
<propertyname="build.classes"value="${build.dir}/classes"></property>
<!--location指定的路径可以根据操作系统生成相应的路径-->
<propertyname="build.classes"location="${build.dir}/classes"></property>
<propertyname="build.src"location="${build.dir}/classes"></property>
<propertyname="build.lib.dir"location="${build.dir}/classes"></property>
<propertyname="execute.class"value="com.zsj.app.HelloWorld"></property>
<propertyname="jar.name"value="com.zsj.app.HelloWorld"></property>
<!--到处环境变量-->
<propertyenvironment="env"></property>
<targetname="test">
<echo>${ant.home}</echo>
<echo>${ant.version}</echo>
<echo>${env.classpath}</echo>
</target>
<!--文件集-->
<filesetid="src.path"dir="src"includes="**/*.java"></fileset>
<filesetdir="src">
<includename="**/*.*"/>
<excludename="**/*.properties"/>
</fileset>
<targetname="init">
<deletedir="${build.dir}"></delete>
<mkdirdir="${build.dir}"/>
<mkdirdir="${build.dir}/src"/>
<mkdirdir="${build.dir}/classes"/>
<mkdirdir="${build.dir}/dist"/>
<!--<delete dir="build"></delete>-->
</target>
<targetname="copySrc"depends="init">
<copytodir="${build.dir}/src">
<filesetrefid="src.path"></fileset>
</copy>
</target>
<targetname="compile"depends="init">
<javacdestdir="${build.dir}/classes"srcdir="src"></javac>
</target>
<targetname="jar"depends="compile">
<jardestfile="${build.dir}/dist/hello.jar"basedir="${build.dir}/classes">
<manifest>
<!--Main-Class指明可以运行的文件-->
<attributename="Main-Class"value="com.zsj.app.HelloWorld"/>
<attributename="Build-By"value="zhangshijie"/>
</manifest>
</jar>
</target>
<targetname="execute"depends="jar,copySrc">
<echo>基于类路劲的classname来完成执行</echo>
<javaclassname="com.zsj.app.HelloWorld"classpath="${build.dir}/classes">
<argvalue="zhangsan"/>
<argvalue="lisi"/>
<argvalue="wangwu"/>
</java>
<echo>基于jar文件执行,未设置fork的时候是ant的环境,设置为true之后用的是JDK的环境</echo>
<javajar="${build.dir}/dist/hello.jar"fork="true">
<argvalue="zhangsan"/>
<argvalue="lisi"/>
<argvalue="wangwu"/>
</java>
</target>
</project>