Dashboard > SpringSide Wiki > ... > Tools > Ant Programming
  SpringSide Wiki Log In | Sign Up   View a printable version of the current page.  
  Ant Programming
Added by SpringSideTeam, last edited by SpringSideTeam on 2006-11-23  (view change)
Labels: 

作者:江南白衣  

Ant 编程指南 

    这篇文档是专门写给那些编程狂热者,在Ant里编程时要留意的重要Task。

    不知为何,老外的各种脚本都写得格外漂亮。从Appfuse里学到很多,在编写SpringSide2.0的构件安装脚本时又被迫自学了不少,这里作下总结。

    如果只说一样最重要的事情,就是ant-contrib的<if> 和 <for>节点,使Ant 拥有了完整的编程能力。

    另外,《The Elements of Ant Style》 是一份重要的文档。

1. 变量

   Ant里的变量有个诡异的特性----一旦被赋值就不会改变,这个特性有时候帮助很大,有时候让人很苦恼,一定要注意。另一样要注意的是,Ant里的变量和其他语言的变量一样,有可效范围。

   1.由命令行赋值

      ant build.xml -Dtomcat.ome

   2.与用户交互输入--Input task

      <input message="请选择一个Target "
               validargs="compile,jar,test"
               addproperty="my.input"/>

    3.从propertis文件读取并存盘 -- propertyfile task    

      <propertyfile file="my.properties">
              <entry key="springside.home" default="."/>
      </propertyfile>

      如果my.properties 不存在,生成my.properties文件,springside.home=.。有一个特别有用的地方:有些properties文件的属性每个开发者都不同,不想放入svn,但又想初始化数值,可以用该命令。

     <propertyfile file="my.properties">
              <entry key="springside.home" value="....."/>
      </propertyfile>

      重新写入配置文件。

2. 流程控制

     如果没有ant-contrib 贡献的<if> 和<for>节点,Ant的可编程性是极低极低的。

 2.1 if task

 ant原来可以在target级进行if判断(unless,if 属性),但实在太不方便了。

2.2 Conditions

但Ant预先封装的一堆condition很是很方便的。这些condition完全从实际出发,包括文件是否存在,http://localhost:8080是否连通都可以作为条件,见Ant的参考手册

2.3 For task

支持"a,b,c,d" 字符串数组循环与目录,Fileset循环。

2.4 Parallel task

Parallel非常有用,比如我想一边开tomcat,一边做别的,就需要使用它,否则就只有用spawn=true属性把tomcat放在后台运行。spawn有很多不好的地方,比如不能即时看到console信息,停止ant运行不能把tomcat关掉等。

Parallel相当于一个容器,放在里面的每个task都会被并行执行。如果想把某几个task顺序执行,用相当于()的Sequential task 包起来。

2.5 Waitfor task

暂停ant执行直到条件符合,比如<waitfor><http url=http://localhost:8080/></waitfor>就会等待tomcat启动后才会继续往下执行。Macrodef task

3. 代码封装

 ant 代码最基本的封装是

 1. ant  task:调用其他脚本的任务,可设定dir 与是否继承本脚本的变量。

 2. antcall task:调用本脚本内其他task,可设置参数。

 3. import task :就像其他语言的include一样,引入其他脚本内容到本脚本里。

1. AntFetchAntCallBack task

   ant-contrib贡献,对应于Ant 与 AntCall。原版只能向被调用函数传递变量,函数执行后没办法return 值。antcallback的语法如下
   <antcallback target="mytarget" return="myresult"/>

2. Macrodef task

   作为最小的封装单位,与以<target>封装,<antcall target="xxx">调用差不太远,细微之处自行体验了。个人比较喜欢用macrodef。

3. Java taskExec task 直接执行Java类 或程序

   注意执行目录的定义,另在Windows下如果要直接运行dos窗口中的命令,以下指令启动默认浏览器访问localhost:

   <exec executable="cmd.exe">
      <arg line="/c start http://localhost:8080"/>
   </exec>   

4. 扩展Ant的Task

   扩展ant task很简单,实现execute 方法执行task,实现setter接口让ant框架执行属性注入。继承

   稍微有点麻烦的是多层嵌套属性的注入。详细请看http://ant.apache.org/manual/developlist.html

   SpringSide 2.0 里很简单的实现了一个XML File Merge的task,见XmlMergeTask.java。

 5. 文件操作

  刚好springside里进行了比较多的文件操作,随便记一下。

  replace 与 copy 时加入filter, 都可以进行字符串替换.

  concat在文件末添加其他文件的内容。

  好困,很多东西没写详细,明天再补充。

Posted by Anonymous at 2006-11-08 18:19 | Permalink
Posted by Anonymous at 2006-11-21 19:10 | Permalink
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 2006-09-07) - Bug/feature request - Contact Administrators
Get SpringSide at SourceForge.net. Fast, secure and Free Open Source software downloads