build.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- @author nassir Wen @version 2.0 @date 2011-6-25 ant 事例 --> <project basedir="." default="build" name="ant"> <property name="debug" value="true" /> <property name="target" value="1.6" /> <property name="source" value="1.6" /> <property name="encoding" value="UTF-8" /> <property name="build.classes.dir" value="build/classes" /> <property name="build.jar.dir" value="build/jar" /> <path id="ant.classpath"> <pathelement location="${build.classes.dir}" /> <!--外部jar引用--> <fileset dir="packages" includes="*.jar" /> </path> <target name="init" depends="clean"> <mkdir dir="${build.classes.dir}" /> <mkdir dir="${build.jar.dir}" /> <copy includeemptydirs="false" todir="${build.classes.dir}"> <!--将资源文件复制到classes目录下 如:log4j.properties--> <fileset dir="src" excludes="**/**/*.java" /> </copy> </target> <target depends="init" name="build"> <echo message="${ant.project.name}: ${ant.file}" /> <javac debug="${debug}" encoding="${encoding}" destdir="${build.classes.dir}" source="${source}" target="${target}"> <src path="src" /> <classpath refid="ant.classpath" /> </javac> <!-- jar包 --> <jar destfile="${build.jar.dir}/anttest.jar" basedir="${build.classes.dir}"> </jar> </target> <target name="clean"> <echo message="clean ${build.classes.dir} and ${build.jar.dir}" /> <delete dir="${build.classes.dir}" /> <delete dir="${build.jar.dir}" /> </target> <!--通过外部执行--> <target name="run_AntTest"> <echo message="run AntTest ${ant.project.name}" /> <java classname="com.ant.anttest.AntTest" classpathref="ant.classpath" fork="true"> <jvmarg value="-Xmx32m" /> </java> </target> <!--通过外部执行--> <target name="run_AntTestArgs"> <echo message="run AntTestArgs ${ant.project.name}" /> <java classname="com.ant.anttest.AntTestArgs" classpathref="ant.classpath" fork="true"> <jvmarg value="-Xmx32m" /> <arg value="com.ant.bean.AntTestBean1st" /> <arg value="com.ant.bean.AntTestBean2nd" /> <arg value="com.ant.bean.AntTestBean3rd" /> </java> </target> </project>
执行run_antTest 、 run_antTestArgs
D:\Workspaces\ant>ant run_AntTest
Buildfile: D:\Workspaces\ant\build.xml
run_AntTest:
[echo] run AntTest ant
[java] ant test start ....
BUILD SUCCESSFUL
Total time: 1 second
-------------------------------------------
D:\Workspaces\ant>ant run_AntTestArgs
Buildfile: D:\Workspaces\ant\build.xml
run_AntTestArgs:
[echo] run AntTestArgs ant
[java] arg: com.ant.bean.AntTestBean1st
[java] AntTestBean1st_9221
[java] arg: com.ant.bean.AntTestBean2nd
[java] AntTestBean2nd_9940
[java] arg: com.ant.bean.AntTestBean3rd
[java] AntTestBean3rd_8050
BUILD SUCCESSFUL
Total time: 1 second