Ant是Java平台下非常棒的批处理命令执行程序能非常方便地自动完成编译测试打包部署等等一系列任务大大提高开发效率如果你现在还没有开始使用Ant那就要赶快开始学习使用使自己的开发水平上一个新台阶
Eclipse中已经集成了Ant我们可以直接在Eclipse中运行Ant
以前面建立的Hello工程为例创建以下目录结构
新建一个buildxml放在工程根目录下buildxml定义了Ant要执行的批处理命令虽然Ant也可以使用其它文件名但是遵循标准能更使开发更规范同时易于与别人交流
通常src存放Java源文件classes存放编译后的class文件lib存放编译和运行用到的所有jar文件web存放JSP等web文件dist存放打包后的jar文件doc存放API文档
然后在根目录下创建buildxml文件输入以下内容
<?xml version=?>
<project name=Hello world default=doc>
<! properies >
<property name=srcdir value=src />
<property name=reportdir value=report />
<property name=classesdir value=classes />
<property name=libdir value=lib />
<property name=distdir value=dist />
<property name=docdir value=doc/>
<! 定义classpath >
<path id=masterclasspath>
<fileset file=${libdir}/*jar />
<pathelement path=${classesdir}/>
</path>
<! 初始化任务 >
<target name=init>
</target>
<! 编译 >
<target name=compile depends=init description=compile the source files>
<mkdir dir=${classesdir}/>
<javac srcdir=${srcdir} destdir=${classesdir} target=>
<classpath refid=masterclasspath/>
</javac>
</target>
<! 测试 >
<target name=test depends=compile description=run junit test>
<mkdir dir=${reportdir}/>
<junit printsummary=on
haltonfailure=false
failureproperty=testsfailed
showoutput=true>
<classpath refid=masterclasspath />
<formatter type=plain/>
<batchtest todir=${reportdir}>
<fileset dir=${classesdir}>
<include name=**/*Test*/>
</fileset>
</batchtest>
</junit>
<fail if=testsfailed>
***********************************************************
**** One or more tests failed! Check the output ****
***********************************************************
</fail>
</target>
<! 打包成jar >
<target name=pack depends=test description=make jar file>
<mkdir dir=${distdir} />
<jar destfile=${distdir}/hellojar basedir=${classesdir}>
<exclude name=**/*Test* />
<exclude name=**/Test** />
</jar>
</target>
<! 输出api文档 >
<target name=doc depends=pack description=create api doc>
<mkdir dir=${docdir} />
<javadoc destdir=${docdir}
author=true
version=true
use=true
windowtitle=Test API>
<packageset dir=${srcdir} defaultexcludes=yes>
<include name=example/** />
</packageset>
<doctitle><![CDATA[<h>Hello test</h>]]></doctitle>
<bottom><![CDATA[<i>All Rights Reserved</i>]]></bottom>
<tag name=todo scope=all description=To do: />
</javadoc>
</target>
</project>
以上xml依次定义了init(初始化)compile(编译)test(测试)doc(生成文档)pack(打包)任务可以作为模板
选中Hello工程然后选择ProjectPropertiesBuildersNew…选择Ant Build
填入NameAnt_BuilderBuildfilebuildxmlBase Directory${workspace_loc:/Hello}(按Browse Workspace选择工程根目录)由于用到了junitjar包搜索Eclipse目录找到junitjar把它复制到Hello/lib目录下并添加到Ant的Classpath中
然后在Builder面板中钩上Ant_Build去掉Java Builder
再次编译即可在控制台看到Ant的输出
Buildfile: F:\eclipseprojects\Hello\buildxml
init:
compile:
[mkdir] Created dir: F:\eclipseprojects\Hello\classes
[javac] Compiling source files to F:\eclipseprojects\Hello\classes
test:
[mkdir] Created dir: F:\eclipseprojects\Hello\report
[junit] Running exampleHelloTest
[junit] Tests run: Failures: Errors: Time elapsed: sec
pack:
[mkdir] Created dir: F:\eclipseprojects\Hello\dist
[jar] Building jar: F:\eclipseprojects\Hello\dist\hellojar
doc:
[mkdir] Created dir: F:\eclipseprojects\Hello\doc
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package example
[javadoc] Constructing Javadoc information
[javadoc] Standard Doclet version _
[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:\eclipseprojects\Hello\doc\stylesheetcss
[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: seconds
Ant依次执行初始化编译测试打包生成API文档一系列任务极大地提高了开发效率将来开发JEE项目时还可加入部署等任务并且即使脱离了Eclipse环境只要正确安装了Ant配置好环境变量ANT_HOME=<Ant解压目录>Path=…;%ANT_HOME%\bin在命令行提示符下切换到Hello目录简单地键入ant即可