分类: Java
2011-10-01 23:35:42
<?xml version="1.0"?> <project name="Hello world" default="doc"> <!-- properies --> <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"/> <!-- 定义classpath --> <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> <!-- 打包成jar --> <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> <!-- 输出api文档 --> <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> |
Buildfile: F:\eclipse-projects\Hello\build.xml init: compile: [mkdir] Created dir: F:\eclipse-projects\Hello\classes [javac] Compiling 2 source files to F:\eclipse-projects\Hello\classes test: [mkdir] Created dir: F:\eclipse-projects\Hello\report [junit] Running example.HelloTest [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec pack: [mkdir] Created dir: F:\eclipse-projects\Hello\dist [jar] Building jar: F:\eclipse-projects\Hello\dist\hello.jar doc: [mkdir] Created dir: F:\eclipse-projects\Hello\doc [javadoc] Generating Javadoc [javadoc] Javadoc execution [javadoc] Loading source files for package example... [javadoc] Constructing Javadoc information... [javadoc] Standard Doclet version 1.4.2_04 [javadoc] Building tree for all the packages and classes... [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes... [javadoc] Generating F:\eclipse-projects\Hello\doc\stylesheet.css... [javadoc] Note: Custom tags that could override future standard tags: @todo. To avoid potential overrides, use at least one period character (.) in custom tag names. [javadoc] Note: Custom tags that were not seen: @todo BUILD SUCCESSFUL Total time: 11 seconds |
Ant依次执行初始化,编译,测试,打包,生成API文档一系列任务,极大地提高了开发效率。将来开发J2EE项目时,还可加入部署等任务。并且,即使脱离了Eclipse环境,只要正确安装了Ant,配置好环境变量ANT_HOME=<Ant解压目录>,Path=…;%ANT_HOME%\bin,在命令行提示符下切换到Hello目录,简单地键入ant即可