Ant全攻略_计算机软件及应用_IT计算机_专业资料_第1页
Ant全攻略_计算机软件及应用_IT计算机_专业资料_第2页
Ant全攻略_计算机软件及应用_IT计算机_专业资料_第3页
Ant全攻略_计算机软件及应用_IT计算机_专业资料_第4页
Ant全攻略_计算机软件及应用_IT计算机_专业资料_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、ant全攻略ant是什么?ant 是种基丁 java 和 xml 的 build具。2卜载、安装ant安装ant下载.zip文件,解压缩到c:ant 1.3(后面引用为%ant_home%)2.1在你运行ant z前需要做一些配置工作。将bin目录加入path环境变量。设定ant.home环境变量,指向你安装ant的目录。在一些os上,ant的脚木可以猗测ant_home (unix和windos nt/2000) 一但最好不要依赖这一特性。可选地,设定java_home环境变量(参考下而的高级小节),该变量应该指向你安装jdk的目录。 注意:不要将ant的ant.jar文件放到jdk/jre

2、的lib/ext目录下。ant是个应用程序,而lib/ext目录是为jdk 扩展使用的(如jce, jsse扩展)。而且通过扩展装入的类会有安全方面的限制。2.2运行ant运行ant非常简单,当你正确地安装ant后,只要输入ant就可以了。n没有指定任何参数时,ant会在当前目录下查询build.xml文件。如果找到了就用该文件作为buildfile。 如果你用-find选项。ant就会在上级忖录中寻找buildfile,直至到达文件系统的根。要想让ant使用其他 的buildtile,可以用参数-buildfile file,这里file指定了你想使用的buildfile。n川以指定执行一个

3、或多个target。当省'峪target时,ant使丿i标签project的default属性所指定的target。 命令行选项总结:ant optionsl target (target? target3.options:-help print this message-projccthclp print project help information-version print the version information and exit-quiet be extra quieterbose be extra verbose-debug print debugging inf

4、ormationcmacs produce logging information without adornments-logfile file use given file for log output-logger elassname the class that is to perform logginglistener classname add an instance of class as a project listener-buildfile file use specified buildfile-find file search for buildfile towards

5、 the root of the filesystem and use the first one found-dproperty=value sei property to value例子ant使用当前h录下的build.xml运行ant,执彳了缺省的targetoant -buildfile tcst.xml使用当前目录-f的test.xml运行ani,执行缺省的targetoant -buildfile tesi.xml disl使用当前目录下的test.xnil运行ant,执行一个叫做dist的target。ant -buildfile test.xml -dbuild=build/c

6、lasses dist便用当前目录下的test.xml运行ant,执行一个叫做dist的target,并设定build属性的值为build/classcs。3 编写 build.xmlant 的 buildfile 是用 xml 写的。每个 buildfile 含有一个 projectobuildfile «|>每个task元素可以冇一个id厲性,可以用这个id值引用指定的任务。这个值必须是唯一的。(详 情请参考下而的task小节)3.1 projectsproject有下面的属性:attribute description requiredname项目名称.no (可选)de

7、fault当没有指定target时使用的缺省target yes (必需)basedir用于计算所有其他路径的基路径。该属性可以被basedir property覆盖。曲覆盖时,该属性被忽略。 如果属性和basedir property都没有设定,就使用buildfile文件的父目录。no (町选)项h的描述以一个顶级的<description>元索的形式出现(参看description小节)。一个项目可以定义一个或多个target。一个target是一系列你想要执行的。执行ant时,你可以选择执行那 个targeto曲没冇给定target时,使用project的default屈性

8、所确定的targeto3.2 targets一个target可以依赖于其他的targeto例如,你町能会有一个target用于编译程序,一个target用于生成町 执行文件。你在生成可执行文件z前必须先编译通过,所以生成町执行文件的targe依赖于编译target. ant 会处理这种依赖关系。然而,应半注意到,ant的depends属性只指定了 target应该被执行的顺序一如果被依赖的target (假定为 a任务或a系列任务)无法运行,这种depends对于指定了依赖关系的target 定为b任务)就没有影响。(个人认为a任务即使不能执行,b任务还会按照口定的规则执行)ant会依照dep

9、ends属性中target出现的顺序(从左到右)依次执行每个targeto然而,耍记住的是只耍 某个target依赖于一个target,后者就会被先执行。<targel name="a"/><targct namc="b" dcpcnds="a'7><tiirget name="c" depends="b'7><target name="d" depends="c,b.a'7>假定我们要执行target de从它的

10、依赖属性來看,你可能认为先执行c,然后b,最后a被执行。错了,c 依赖于b, b依赖于a,所以先执行a,然后b,然后c,最后d被执行。一个target只能被执行一次,即时有多个target依赖于它(看上面的例子)。如果(或如果不)某些属性被设定,才执行某个targeto这样,允许根据系统的状态(java version, os,命 令行属性定义等等)来更好地控制build的过程。要想让一个largei这样做,你就应该在target元素中,加 入if (或unless)属性,带上wrget因该有所判断的属性。例如:<targct namc="build-inodulc-a&quo

11、t; if="modulc-a-prcscnt"/><target name=nbuild-own-fake-module-a" unless="module-a-present"/>如果没有if或unless属性,target总会被执行"可选的description属性可用来提供关于target的一行描述,这些描述可由-projecthelp命令行选项输出。将你的islamp risk在个所谓的初始化largel是很好的做法,其他的largel依赖这个初始化targeto耍确保 初始化target是出现在其他tar

12、get依赖农中的第一个target。在本手册中大多数的初始化target的名字是"i n i i ”。【arget冇卜-面的属性:attribute description requiredname target的名字yes (必需)depends用逗号分隔的target的名字列表,也就是依赖表。no (可选)if执行target所需要设定的属性名。no (可选)unless执行target需要清除设定的属性名。no (叮选)description关t target功能的简短描述。no (可选)3.3 tasks一个task是一段可执行的代码。一个task可以冇多个属性(如果你愿意的

13、话,可以将其称之为变暈)。属性只可能包含对property的引用。 这些引用会在task执行前被解析。卜而是task的-般构造形式:<name attribute l="valuel" attribute2="value2" . />这里name是task的名字,attributen是属性名,valuen是属性值。有一套内凰的(built-in) task,以及一些lj选task,但你也川以编写自己的task。所冇的task都冇一个task名字属性。ant用属性值來产生日志信息。可以给task赋一个id属性:<taskname id=&

14、quot;taskid" . />这m taskname是lask的名字,而taskid是这个lask的唯-标识符。通过这个标识符,你可以在脚本中引用 相应的taske例如,在脚木中你可以这样:<script. >taskl.sctfoo("bar");</script>设定某个task实例的foo属性。在另一个(ask中(用java编写),你可以利用下面的语句存取相应的实例。 projcct.gctrcfcrcncc(ntask 1").注意i:如果taskl还没有运行,就不会被生效(例如:不设定属性),如果你在随后配置它

15、,你所作的一 切都会被覆盖。注意2:未來的am版木可能不会兼容这里所提的属性,因为很有可能根木没有(ask实例,只有proxieso3.4 properties 一个project可以有很多的propertieso可以在buildfile中川property task来设泄,或在ant之外设定。一 个property有一个名字和一个值。property可用于task的属性值。这是通过将属性名放在”$”和之间 并放在属性值的位置来实现的。例如如果有一个property builddir的值是"build",这个property就可用丁-属 性值:$ builddir /cla

16、sses« 这个值就可被解析为 build/classes<>内置属性如果你便丿u 了vpropcrty> task定义了所有的系统属性,ant允许你使用这吐属牲。例女l【,$os.namc对应操 作系统的名字。要想得到系统属性的列表可参考the javadoc of systcm.gctpropcilicso除了 java的系统属性,ant还定义了一些h己的内置属性:basedir project基目录的绝对路径( "j<project>的basedii属性样)。ant.file buildfile 的绝对路径。ant.vcrsion ant

17、 的版本。 当前执彳j的 project 的名字;h|vprojecl>的 name 属性设定.ant.java.version ant 检测到的 jvm 的版本;目前的值有”1.1”, t.2”,”1.3” and "1.4".例子<project name=,'myprojecth default=hdisth basedir=n.">v! set global properties for this build -><property name="src" value=

18、h.'7><property name=hbuildm value=mbuild,/><property name=hdistn value=ndist7><target name=hinith><! create the time stamp ><tstamp/><! create the build directory structure used by compile ><mkdir dir="s build 7></targct><target name=hco

19、mpileh depends=ninith><! compile the java code from $src) into $build><javac srcdir="$src)h destdir=n$ build )7></targct><target name=mdisth depends=hcompilem>v! create the distribution directory ><mkdirdir=,$dist)/lib,7><!- put everything in $build) into

20、the myproject-sdstamp).jar file -> vjar jarfile=”$dist)/lib/myproject-$(dstamp.jar" basedir="$build)v> </target> <iargel name=hcleann><!- delete the $build) and $dist) directory trees -><dclctc dir="$ build n/><delete dir=m$dist)7></targct><

21、;/project>3.5 path-like structures你可以用和"”作为分隔符,指定类似path和classpath的引用。ant会把分隔符转换为当前系统 所用的分隔符。当需要指定类似路径的值时,可以使用嵌套元素。-般的形式是<classpath><pathelement path=h$classpath)h/><pathelement location=,lib/helper.jarn/></classpath>location属性指定了相对于project基目录的一个文件和目录,而path属性接受逗号或分号分隔的

22、一个位 置列表。path属性一般用作预定义的路径其他情况下,应该用多个location属性。为简洁起见,classpath标签支持自己的path和location属性。所以:<classpath><pathelement path=n$classpath )7></classpath>可以被简写作:<classpath path=1 f$classpath17>也町通过<fiieset>元索指左路径。构成一个fileset的多个文件加入path-like structure的顺序是未定的。<classpath><pa

23、thelement path=>9$classpath17>vfileset dir=',iib,><include na me=1 * * */*.j ar * 7></fi!eset><pathclcmcnt location二"classes"/</classpath>上面的例子构造了一个路径值包括:sclasspath)的路径,跟着lib目录下的所有jar文件,接着是classes 目录。如果你想在多个task中使用相同的path-likc structure,你以用<path>元素定义

24、他们(与target同级),然 后通过id属性引用一一参考referencs例子。path-like structure可能包括对另一个path-like structurede的引用(通过嵌套vpath>元素):<path id=nbase.pathm><pathclcnicnt path=n$classpath)v><fiieset dir="lib,><include name=n*/*.jar,7></fileset><pathelement location=hclasses7></path

25、><path id=htests.paihm><path refkl=,base.pa(h,7><pathclcmcnl location=htcstclasscs,7></path>前面所提的关t<classpath>的简洁写法对t*<path>也是有效的,如:<path id二"tests.path"<path refid=mbase.pathm/><pathclcmcnt location=',tcstclasscs,7></path>可写成

26、:<path id=hbase.pathn path=n$ classpath) h/>命令行变量有些lask可接受参数,并将其传递给另一个进程。为了能在变量中包含空格字符,可使用嵌套的arg元索。attribute description requiredvalue 一个命令彳亍变量;可包含空格字符。只能用一个line空格分隔的命令行变量列表。file作为命令行变量的文件名;会被文件的绝对名替代。path 一个作为单个命令行变量的path-like的字符串;或作为分隔符,anl会将其转变为特定平台的分隔符。例子<arg value-ah/>是一个含有空格的单个的命令行

27、变量。<arg line="-l -a"/>是两个空格分隔的命令行变量。<arg path='7dir;/dir2:dir3'7>是一个命令行变虽:,其值在dos系统上为dir;dir2;dir3;在unix系统上为/dir:/dir2:/dir3。referencesbuildfile元索的id属性可用來引用这些元素。如果你需要一遍遍的复制相同的xml代码块,这一属性就很 有用如多次使用<classpath>结构。下面的例子:<projcct><target ><rmic .><

28、classpath><pathelement location=hlib/7><pathclcmcnt path=h$java.class.path /h/><pathelement path=nsadditional.path f7></classpath></rmic></target><target. ><javac ><classpath><pathclcmcnt location=hlib/v><pathelement path=n$java.class

29、.path t!><paihelemeni path=,$ add i tional .path m/></classpath></javac></targct></project>可以写成如卜-形式:<projcct. ><path id=hproject.class.pathh><pathclcmcnt location=hlib/h/><pathelement path=n$java.class.path /”/><pathelement path=hsaddition

30、al.path) "/></path><target. ><rmic ><classpath refid二”project.class.path”/</rmic></targct><target. ><javac ><classpath refid=,project.class.pathh/></javac></target></project>所有使用patternsets, filesets或path-like structures嵌套元

31、素的task也接受这种类型的引用。ant全攻略(续)ant中的内置任务4.1 file (directory)类4.1mkdirn创建一个目录,如果他的父目录不存在,也会被同时创建。n例子:<mkdir dir="build/classes'7>n说明:如果build不存在,也会被同时创建4.1.2 copyn拷贝一个(组)文件、目录n例子:1. 拷贝单个的文件:<copy file="myfile.txt" tofile="niycopy.txt'7>2. 拷贝单个的文件到指定目录下<copy file=&

32、quot;myfile.txt" todir=',./some/other/dir'7>3. 拷贝一个目录到另外一个目录下<copy todir="./new/dir"><fileset dir=,'src_dir"/></copy>4. 拷贝一批文件到指定目录下<copy todir=h./dcst/dirn><fileset dir=hsrc dir,r><exclude name=,f*/*.java7></filcsct></c

33、opy><copy todir=,./dest/dir,><fileset dir=msrc_dirh excludes=n*/*.javav></copy>5. 拷贝一批文件到指定目录下,将文件名后增加bak后缀<copy todir="./backup/dir"><fileset dir="src_dir"/><mapper type=,glob" from=”*” to=m*.bak,/></copy>6. 拷贝一组文件到指定目录下,替换其中的标签内

34、容<copy todir=n./backup/dirn><fileset dir="src_dir"/><filterset><filter token二"title" valuc=hfoo barh/></filterset></copy>4.1.3 deleten删除一个(组)文件或者目录n例子1. 删除一个文件<delete file="/lib/ant.jar"/>2. 删除指定目录及其子目录<delete dir=hiib'7&

35、gt;3. 删除指定的一组文件<delete><fileset dir=',.h includes=n*/*.bak'7></delete>4. 删除指定目录及其子目录,包括他自己<dclctc includceniptydirs=mtruch><fileset dir=nbuild7></dclctc>4.1.4 moven移动或重命名一个(组)文件、目录n例子:1. 移动或重命名一个文件<movc file=,file.orign tofilc=hfilc.movcd,7>2. 移动或重命名

36、一个文件到另一个文件夹下面<move file="file.orig" todir="dir/to/move/to'7>3. 将一个目录移到另外一个目录下<move todir=,new/dir/to/move/lo,><fileset dir="src/dir'7></move>4. 将一组文件移动到另外的目录下<movc todir=hsomc/ncw/dirm><filesei dir=nmy/src/dirn><include name=n */*.ja

37、r *7><cxcludc namc="水*/ant.jar"/</fileset></niovc>5. 移动文件过程中增加.bak后缀<move todir=nmy/src/dirm><filcsct dir=nmy/src/dirn><exclude name=h*/*.bak7></filcsct><mapper type=hglobm from=h*h to=h*.biik*7></move>ant全攻略-java相关任务4.2.1 javacn编译java原

38、代码n例子1. <javac srcdir=,ssrc)hdestdir="$ build hclasspath=mxyz.jarndebug="on"/>编译$src目录及其子目录下的所有java文件,class文件将放在$ build指定的目录下,classpath农示 需要用到的类文件或者目录,debug设置为on表示输出debug信息2. <javac srcdir=,$sre :$(src2hdestdir='*$ build'1includes=nniypackage/pl/*,mypackage/p2/*nexcl

39、udes=mmypackage/pl/testpackage/*”classpath=nxyz.jar'rdebug=mon"/>编译$src和$src2( g录及其子目录下的所有java文件,但是package/pl/*,mypackage/p2/*将被编译, 而mypackage/pl/testpackage/*将不会被编译“ class文件将放在$ (build)指定的目永下,classpath表示 需要用到的类文件或者忖录,debug设置为on表示输出debug信息3. <property name=nclasspathh value=h.;./xml-a

40、pis.jar;.,/lib/xbean.jar;./easypo.jar7><javac srcdii-nssrc)hdcstdir=',$src)"classpath 二"$ (classpathhdebugion”/>路径是在properly中定义的4.2.2 javan执行指定的java类n例子:1. <java classname=,testmain,><classpath><pathelement localionnfisi/lesl.jar”/<pathclcmcnt path=m$java.cla

41、ss.path )h/></classpath></java>classname中指定要执行的类,cl asspath设立婆使用的环境变虽2. <path id=mproject.class.pathm><pathclcmcnt location="iib/,7><pathelement path=n$ java.class.paih/”/><pathclcmcnt path=h$ additional .path *7></path><targct. ><rmic .>

42、<classpath refid二”project.class.path"/</rmic></target>ant全攻略-打包相关任务4.3打包相关4.3.1 jarn将一组文件打包n例子:1. <jardestfile="$dist)/lib/app.jar" basedir=,'$build)/classes"/>将s build (/classes b*面的所有文件打包到$dist)/lib/app.jar 中2. <jar destfile="sdist )/lib/app.jar

43、"basedir="$ build/classes"inckides=hmypackage/test/*hexcludes* */test.classh/>将sbuild/classes下面的所有文件打包至u$dist)/lib/app.jar中,但是包括mypackage/test /所有文件不包括 所有的 tcst.class3. <jar destfile="$dist/lib/app.jar"basedir="$ build (/classes"includcs=hmypackagc/tcst/*nexc

44、ludes=h*/test.classhmanifcst=my.mf,/>manifest属性指定自己的meta-inf/manifest.mf文件,而不是由系统生成4.3.2 warn对jar的扩展,用于打包web应用n例子:n假设我们的文件目录如下:thirdparty/libs/jdbcl .jarthirdparty/libs/jdbc2.jarbuild/main/com/myco/myapp/servlei.classsrc/metadata/myapp.xmlsrc/html/myapp/indcx.htmlsrc/j sp/myapp/front .j spsrc/gra

45、phics/imagcs/gifs/small/logo.gifsrc/graphics/images/gifs/large/logo.gifn下而是我们的任务的内容:<war destfile=hmyapp.warh webxml二"src/metadata/myapp.xml<filesel dir=hsrc/hlml/myapp7><filcsct dir=hsrc/jsp/myapp,7><lib dir="thirdpiirty/libs"><exclude name=mjdbc 1 .jar7>&l

46、t;/lib><classes dir=,build/main,7><zipfileset dir=hsrc/graphics/images/gifsmprefix=himages,7></war>n完成后的结果:web-inf/web.xmlweb-inf/lib/jdbc2,jarweb-inf/classes/com/myco/myapp/servlet.classmeta-inf/manifest.mfindex.htmlfront.jspimages/sinall/logo.gifimages/large/logo.gif4.3.3 earn

47、用于打包企业应用n例子veaidestfile=”$builddir/myapp.ear” appxml=”$srcdir/metadata/applicationxml”><fileset dir="$ build.dir" includes="*.jar,*.war"/></ear>ant全攻略实现时间戳4.4时间戳在生成环境中使用当前时间和日期,以某种方式标记某个生成任务的输出,以便记录它是何时生成的,这 经常是可取的。这可能涉及编辑一个文件,以便插入一个字符串来指定日期和时间,或将这个信息合并到 jar或zip文件的文件名中。这种需要是通过简单但是非常有用的tstamp任务来解决的。这个任务通常在某次生成过程开始时调用,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论