随笔 - 45, 文章 - 6, 评论 - 4, 引用 - 0
数据加载中……

Ant的build.xml

新建一个build.xml,放在工程根目录下。build.xml定义了Ant要执行的批处理命令。虽然Ant也可以使用其它文件名,但是遵循标准能更使开发更规范,同时易于与别人交流。

通常,src存放Java源文件,classes存放编译后的class文件,lib存放编译和运行用到的所有jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文档。

然后在根目录下创建build.xml文件,输入以下内容:


Xml代码
  1. <?xml version="1.0"?>  
  2. <project name="Hello world" default="doc">  
  3.   
  4. <!-- properies -->  
  5.     <property name="src.dir" value="src" />  
  6.     <property name="report.dir" value="report" />  
  7.     <property name="classes.dir" value="classes" />  
  8.     <property name="lib.dir" value="lib" />  
  9.     <property name="dist.dir" value="dist" />  
  10. <property name="doc.dir" value="doc"/>  
  11.   
  12.     <!-- 定义classpath -->  
  13.     <path id="master-classpath">  
  14.         <fileset file="${lib.dir}/*.jar" />  
  15.         <pathelement path="${classes.dir}"/>  
  16.     </path>  
  17.   
  18.     <!-- 初始化任务 -->  
  19.     <target name="init">  
  20.     </target>  
  21.   
  22.     <!-- 编译 -->  
  23.     <target name="compile" depends="init" description="compile the source files">  
  24.         <mkdir dir="${classes.dir}"/>  
  25.         <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">  
  26.             <classpath refid="master-classpath"/>  
  27.         </javac>  
  28.     </target>  
  29.   
  30.     <!-- 测试 -->  
  31.     <target name="test" depends="compile" description="run junit test">  
  32.         <mkdir dir="${report.dir}"/>  
  33.         <junit printsummary="on"  
  34.                 haltonfailure="false"  
  35.                 failureproperty="tests.failed"  
  36.                 showoutput="true">  
  37.             <classpath refid="master-classpath" />  
  38.             <formatter type="plain"/>  
  39.             <batchtest todir="${report.dir}">  
  40.                 <fileset dir="${classes.dir}">  
  41.                     <include name="**/*Test.*"/>  
  42.                 </fileset>  
  43.             </batchtest>  
  44.         </junit>  
  45.         <fail if="tests.failed">  
  46.          ***********************************************************   
  47.          ****   One or more tests failed!   Check the output ...   ****   
  48.          ***********************************************************   
  49.         </fail>  
  50.     </target>  
  51.   
  52.     <!-- 打包成jar -->  
  53.     <target name="pack" depends="test" description="make .jar file">  
  54.      <mkdir dir="${dist.dir}" />  
  55.         <jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">  
  56.             <exclude name="**/*Test.*" />  
  57.             <exclude name="**/Test*.*" />  
  58.         </jar>  
  59.     </target>  
  60.   
  61.     <!-- 输出api文档 -->  
  62.     <target name="doc" depends="pack" description="create api doc">  
  63.      <mkdir dir="${doc.dir}" />  
  64.      <javadoc destdir="${doc.dir}"  
  65.             author="true"  
  66.             version="true"  
  67.             use="true"  
  68.             windowtitle="Test API">  
  69.             <packageset dir="${src.dir}" defaultexcludes="yes">  
  70.                 <include name="example/**" />  
  71.             </packageset>  
  72.             <doctitle><![CDATA[<h1>Hello, test</h1>]]></doctitle>  
  73.             <bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom>  
  74.             <tag name="todo" scope="all" description="To do:" />  
  75.         </javadoc>  
  76.     </target>  
  77. </project>  

posted on 2009-05-01 14:21 liyang 阅读(222) 评论(0)  编辑  收藏