新建一个build.xml,放在工程根目录下。build.xml定义了Ant要执行的批处理命令。虽然Ant也可以使用其它文件名,但是遵循标准能更使开发更规范,同时易于与别人交流。
通常,src存放Java源文件,classes存放编译后的class文件,lib存放编译和运行用到的所有jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文档。
然后在根目录下创建build.xml文件,输入以下内容:
- <?xml version="1.0"?>
- <project name="Hello world" default="doc">
-
-
- <property name="src.dir" value="src" />
- <property name="report.dir" value="report" />
- <property name="classes.dir" value="classes" />
- <property name="lib.dir" value="lib" />
- <property name="dist.dir" value="dist" />
- <property name="doc.dir" value="doc"/>
-
-
- <path id="master-classpath">
- <fileset file="${lib.dir}/*.jar" />
- <pathelement path="${classes.dir}"/>
- </path>
-
-
- <target name="init">
- </target>
-
-
- <target name="compile" depends="init" description="compile the source files">
- <mkdir dir="${classes.dir}"/>
- <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">
- <classpath refid="master-classpath"/>
- </javac>
- </target>
-
-
- <target name="test" depends="compile" description="run junit test">
- <mkdir dir="${report.dir}"/>
- <junit printsummary="on"
- haltonfailure="false"
- failureproperty="tests.failed"
- showoutput="true">
- <classpath refid="master-classpath" />
- <formatter type="plain"/>
- <batchtest todir="${report.dir}">
- <fileset dir="${classes.dir}">
- <include name="**/*Test.*"/>
- </fileset>
- </batchtest>
- </junit>
- <fail if="tests.failed">
- ***********************************************************
- **** One or more tests failed! Check the output ... ****
- ***********************************************************
- </fail>
- </target>
-
-
- <target name="pack" depends="test" description="make .jar file">
- <mkdir dir="${dist.dir}" />
- <jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">
- <exclude name="**/*Test.*" />
- <exclude name="**/Test*.*" />
- </jar>
- </target>
-
-
- <target name="doc" depends="pack" description="create api doc">
- <mkdir dir="${doc.dir}" />
- <javadoc destdir="${doc.dir}"
- author="true"
- version="true"
- use="true"
- windowtitle="Test API">
- <packageset dir="${src.dir}" defaultexcludes="yes">
- <include name="example/**" />
- </packageset>
- <doctitle><![CDATA[<h1>Hello, test</h1>]]></doctitle>
- <bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom>
- <tag name="todo" scope="all" description="To do:" />
- </javadoc>
- </target>
- </project>