说他无所不能,好像有点夸张,但是用过Ant之后,感觉真的是只有想不到没有作不到.Ant,原作者选择他作为软件名字的意思是指"令一个简洁的工具"(Another Neat Tool),而这个真正的名字现在去很少为人所知,但这丝毫不影响他成为最优秀的构建工具.
现在开始我将进入一个"蚂蚁"的世界,通过例子,真真正正去了解他!
文章参考资料可以到http://www.manning.com/antbook去下载
Ant的最好学习资料<<使用Ant进行Java开发>>
Ant的官方网站: http://ant.apache.org/
Ant的最新版本:Ant 1.6.5
本文所有的例子运行的环境:JDK1.4.2,Ant1.6.2,eclipse3.0
一.使用Ant运行Java程序
我们先从简单的Hello学起,目录结构如下
project--
|src--
| |--org.ant.chapter1.Hello
|bin
|build.xml
以后的例子大多采用此目录结构,特例会额外声明 build.xml文件
xml version ="1.0" ?> < project name ="project" default ="run" > < target name ="compile" > < javac destdir ="bin" srcdir ="src" > javac > target > < target name ="run" depends ="compile" > < java classname ="org.ant.chapter1.Hello" > java > target > project >
从结构来看构建文件很简单,里面的内容大家也一定能够看得懂,可以看出Ant的核心任务就是target,一个Ant文件有多个target组成,而这些target之间,又有相互的依赖关系--depends,运行的时候默认运行project中指定的target. javac--编译java文件 java--运行java文件 使用eclipse中集成的Ant运行build.xml文件(当然,也可以将ANT_HOME加到Path中,在命令行中运行)
Buildfile: D:\MyEclipse\workspace\sad\build.xml compile: run: [java] Working directory ignored when same JVM is used. [java] Could not find org.ant.chapter1.Hello. Make sure you have it in your classpath [java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:166 ) [java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:705 ) [java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:177 ) [java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:83 ) [java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275 ) [java] at org.apache.tools.ant.Task.perform(Task.java:364 ) [java] at org.apache.tools.ant.Target.execute(Target.java:341 ) [java] at org.apache.tools.ant.Target.performTasks(Target.java:369 ) [java] at org.apache.tools.ant.Project.executeTarget(Project.java:1214 ) [java] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:379 ) [java] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:135 ) BUILD SUCCESSFUL Total time: 703 milliseconds
,java入门的经典错误,ClassNotDefException,可见是classpath设置问题,而观察得到compile成功运行,所以我们在run-target里面加入classpath的配置
xml version ="1.0" ?> < project name ="project" default ="run" > < target name ="compile" > < javac destdir ="bin" srcdir ="src" > javac > target > < target name ="run" depends ="compile" > < java classname ="org.ant.chapter1.Hello" > < classpath path ="bin" > classpath > java > target > project >
运行
Buildfile: D:\MyEclipse\workspace\sad\build.xml compile: run: [java] Hello World! BUILD SUCCESSFUL Total time: 672 milliseconds
成功!!第一个Ant应用完成,有人会说:用IDE运行岂不是更简单,但是你要知道运行java程序只是Ant的一个小小的功能,后面我们会看到Ant的更强大的功能!
下一篇文章将介绍java程序运行的扩展及用Ant运行tomcat! java程序运行的扩展
1.带有参数的应用程序运行
Ant在<java></java>任务中提供了<arg></arg>元素,<arg></arg>有四种属性value,file,line,path
public class Hello { public static void main(String[] args) { System.out.println("Hello " + args[0 ]); File file = new File(args[1 ]); String[] filename = file.list(); for (int i = 0 ; i < filename.length; i++) { System.out.println(filename[i]); } } }
build.xml
xml version ="1.0" ?> < project name ="project" default ="run" > < property name ="run.classpath" value ="bin" > property > < target name ="compile" > < javac destdir ="bin" srcdir ="src" > javac > target > < target name ="run" depends ="compile" > < java classname ="org.ant.chapter1.Hello" > < classpath path ="${run.classpath}" > classpath > < arg value ="Ant" /> < arg file ="D:\rag" /> java > target > project >
Buildfile: D:\MyEclipse\workspace\sad\build.xml compile: run: [java] Hello Ant [java] hello.dat BUILD SUCCESSFUL Total time: 734 milliseconds
2.控制新的JVM
一般的<java></java>任务都运行在当前的JVM中,单一些特定的情况下将Ant运行在新的JVM下面,这时只需要将<java></java>中的一个属性fork设置为true就可以了. 我们知道,java命令提供了许多的运行参数,用于指定JVM的属性,同样在Ant也提供相应的属性,看例子:
?xml version ="1.0" ?> < project name ="project" default ="run" > < property name ="run.classpath" value ="bin" > property > < property name ="Search.JVM.extra.args" value ="-Xincgc" > property > < target name ="compile" > < javac destdir ="bin" srcdir ="src" > javac > target > < target name ="run" depends ="compile" > < java classname ="org.ant.chapter1.Hello" fork ="true" maxmemory ="64m" > < classpath path ="${run.classpath}" > classpath > < jvmarg line ="${Search.JVM.extra.args}" /> < arg value ="Ant" /> < arg file ="D:\rag" /> java > target > project >
3.运行jar文件,使用failonerror处理错误
<java></java>同样提供了运行jar文件的属性
MANIFEST.MF
Mainfest-Version: 1.0 Created-By: myth Sealed: false Main-Class: org.ant.chapter1.Hello
build.xml
xml version ="1.0" ?> < project name ="project" default ="run" > < property name ="run.classpath" value ="bin" > property > < property name ="Search.JVM.extra.args" value ="-Xincgc" > property > < target name ="compile" > < javac destdir ="bin" srcdir ="src" > javac > target > < target name ="jar" depends ="compile" > < jar destfile ="test.jar" update ="true" manifest ="MANIFEST.MF" > < fileset dir ="bin" > < include name ="**/*.class" /> fileset > jar > target > < target name ="run" depends ="jar" > < java fork ="true" maxmemory ="64m" jar ="test.jar" > < classpath path ="${run.classpath}" > classpath > < jvmarg line ="${Search.JVM.extra.args}" /> < arg value ="Ant" /> < arg file ="D:\rag" /> java > target > project >
在某些情况下,我们不希望由于一些不重要的任务构建失败,而导致整个构建的失败,所以Ant提供了一个共同的属性-failonerror,多数任务的默认值为failοnerrοr="true",既当此任务构建失败时,失败信息会传递给控制台,并导致build failed,failonerror只支持在新的JVM里运行.
< target name ="run" depends ="jar" > < java fork ="true" maxmemory ="64m" jar ="test.jar" failonerror ="false" > < classpath path ="${run.classpath}" > classpath > < jvmarg line ="${Search.JVM.extra.args}" /> < arg value ="Ant" /> < arg file ="D:\rag" /> java > target >
可以看出虽然run构建失败,但是Ant还是执行了,原来的jar文件,并且BUILD SUCCESSFUL!!
使用Ant运行tomcat Ant使用<exec></exec>任务运行本地程序,先看一个例子:
xml version ="1.0" ?> < project name ="project" default ="run" > < target name ="run" > < exec executable ="cmd" > < arg value ="/C a.bat" /> exec > target > project >
a.bat
@echo off echo Hello >> a.txt
运行完后,会在根目录生成a.txt文件,里面内容为Hello 下面我们来运行tomcat
xml version ="1.0" ?> < project name ="project" default ="tomcat-start" > < property name ="tomcat.dir" value ="c:/Tomcat5" > property > < target name ="tomcat-start" > < exec dir ="${tomcat.dir}/bin" executable ="cmd" > < env key ="CATALINA_HOME" path ="${tomcat.dir}" /> < arg value ="/C startup.bat" /> exec > target > < target name ="tomcat-stop" > < exec dir ="${tomcat.dir}/bin" executable ="cmd" > < env key ="CATALINA_HOME" path ="${tomcat.dir}" /> < arg value ="/c shutdown.bat" /> exec > target > project >
四.使用Ant进行Junit测试 我们除了使用java来直接运行junit之外,我们还可以使用junit提供的junit task与ant结合来运行。涉及的几个主要的ant task如下: l <junit></junit>,定义一个junit task l <batchtest></batchtest>,位于<junit></junit>中,运行多个TestCase l <test></test>,位于<junit></junit>中,运行单个TestCase l <formatter> </formatter> ,位于<junit></junit>中,定义一个测试结果输出格式 l <junitreport></junitreport>,定义一个junitreport task l <report></report>,位于<junitreport></junitreport>中,输出一个junit report 运行Junit需要jakarta-ant-1.4-optional.jar和Junit.jar包,因为这两个包用于支持ant task--<junit></junit>的,所以不能在build.xml文件中加载,需要将他们放到ANT_HOME中去.使用eclipse可以按照一下步骤加入: Windows-Preference-Ant-Runtime-Ant Home Entries 下面看一个Junit测试的例子:
xml version ="1.0" ?> < project name ="project" default ="junit" < property name ="run.classpath" value ="bin" > property > < property name ="run.srcpath" value ="src" > property > < property name ="test.srcpath" value ="test" > property > < property name ="lib.dir" value ="lib" /> < path id ="compile.path" > < pathelement location ="${lib.dir}/junit-3.8.1.jar" /> < pathelement location ="${lib.dir}/log4j-1.2.8.jar" /> path > < target name ="compile" > < javac destdir ="${run.classpath}" srcdir ="${run.srcpath}" classpathref ="compile.path" /> < javac destdir ="${run.classpath}" srcdir ="${test.srcpath}" classpathref ="compile.path" /> target > < target name ="junit" depends ="compile" > < junit printsummary ="true" > < classpath path ="${run.classpath}" > classpath > < test name ="org.ant.test.Test1" > test > junit > target > project >
可以看出Junit的使用基本和java差不多, printsummary允许输出junit信息,当然Ant提供formatter属性支持多样化的junit信息输出.Ant包含三种形式的formatter:
brief:以文本格式提供测试失败的详细内容;
plain:以文本格式提供测试失败的详细内容以及每个测试的运行统计;
xml:以xml格式提供扩展的详细内容,包括正在测试时的Ant特性,系统输出,以及每个测试用 例的系统错误.
使用formatter时建议将printsummary关闭,因为他可能对formatter的生成结果产生影响,并多生成一份同样的输出.当然我们可以使用formatter将输出结果显示在console中:
<formatter type="brief" usefile="false"> </formatter> Junit支持多个formatter同时存在: <formatter type="brief" usefile="false"> </formatter> <formatter type="xml"> </formatter> 使用xml我们可以得到扩展性更强的信息输出,这时在<test></test>中要设定todir来指定xml的输出路径. 在通常情况下我们不可能一个一个来处理junit,所以Ant提供了<batchtest></batchtest>,可以在他里面嵌套文件集(fileset)以包含全部的测试用例. 对于大量的用例,使用控制台输出,或者使用文件或xml文件来作为测试结果都是不合适的,Ant提供了<junitreport></junitreport>任务使用XSLT将xml文件转换为HTML报告.该任务首先将生成的XML文件整合成单一的XML文件,然后再对他进行转换,这个整合的文件默认情况下被命名为:TESTS-TestSuites.xml.
"${test.xml}" > "${test.xml}" > "TEST-*.xml" /> "frames" todir="${test.report}" />
<report></report>元素指示转换过程中生成有框架(frames)或者无框架的类似与javadoc格式的文件,并保存到todir所在的目录下面.(由于xalan对于JDK1.4以上的版本存在问题,所以要生成HTML文件需要以下步骤:现在最新的xalan,在%JAVA_HOME%/jre/lib中建立文件夹endorsed.将xalan中的jar文件copy到里面). 下面看一个完整的例子:
xml 代码
<? xml version ="1.0" ?> < project name ="project" default ="junit" > < property name ="run.classpath" value ="bin" > </ property > < property name ="run.srcpath" value ="src" > </ property > < property name ="test.srcpath" value ="test" > </ property > < property name ="test.xml" value ="xml" > </ property > < property name ="test.report" value ="report" > </ property > < property name ="lib.dir" value ="lib" />