转自:http://www.cnblogs.com/QAZLIU/p/3732329.html?utm_source=tuicool&utm_medium=referral

build.xml

<?xml version="1.0"?>
    <project name="ForTest" default="build" >
    <property file="build.properties"></property>
        <!-- import the ant contrib package for using the for or foreach -->
        <taskdef resource="net/sf/antcontrib/antlib.xml"/>  
        <!-- for achieving the traversal of folder with "foreach" tag -->
        <target name="foreach">
                <echo message="Folders in the directory are:"/>
                <foreach target="delete_file" param="dir.name">
                    <path>
                        <!--<dirset dir="${file.dir}" includes="*"/>-->
                        <fileset dir="${file.dir}" includes="jar.zip" ></fileset>
                    </path>
                </foreach>
            </target>
        <!-- for achieving the traversal of folder with "for" tag -->
        <target name="for">
            <echo message="Folders in the directory are:"/>
            <for param="dir.name">
                <path>
                     <dirset dir="${file.dir}" includes="*" />
                     <fileset dir="${file.dir}" includes="*" ></fileset>
                 </path>
                <sequential>
                    <echo message="@{dir.name}"/>
                </sequential>
            </for>
        </target>
            <!-- print the file under the folder-->
           <target name="list.dirs">
                <echo message="${dir.name}"/>
           </target>
        
        <!---delete file -->
         <target name="delete_file">
             <delete file="${dir.name}">
                     
             </delete>
         </target>
            
            <target name="build" depends="foreach" description="Test For loop"/>
    </project>

build.properties

file.dir=G:\\_files

我先解释一下这个ant的运行顺序:

<project name="ForTest" default="build" >

先由这句入口找到build这个target。

也就是

<target name="build" depends="foreach" description="Test For loop"/>

这一句依赖foreach这个target,会找到

<target name="foreach">

一句一句执行,当执行到

<foreach target="delete_file" param="dir.name">

回去找delete_file这个target,也就是

<target name="delete_file">。

注意:

> <fileset dir="${file.dir}" includes="jar.zip" ></fileset>这句是要找出想要删除的zip包,在这里也就是jar.zip

> 现在脚本中用的遍历方式是ant contrib包下的foreach的方式遍历的。for的方式没有用到但是还是写出来了。

> <taskdef resource="net/sf/antcontrib/antlib.xml"/>  加上这一句才可以用for或者是foreach的方式遍历(有多种方法引入,share一个网址:http://blog.csdn.net/sodino/article/details/16923615)

>  这里面可能还比较疑惑的就是:红色标注的地方,这个参数也就是遍历的时候用到的。

<delete file="${dir.name}">这一句中的dir.name用的是用

<foreach target="delete_file" param="dir.name">遍历出来的文件名。

其他:http://blog.csdn.net/u012398902/article/details/51363549

最新文章

  1. MySQL数据库权限操作指南
  2. JavaScript-navigator_userAgent-编写一段代码能够区分浏览器的主流和区分
  3. springmvc入门基础之注解和参数传递
  4. SQL SERVER--单回话下的死锁
  5. Jenkins 插件 CIFS
  6. mongodb 监控分析命令
  7. Linux内核中双向链表的经典实现
  8. sdut Message Flood(c++ map)
  9. CSDN博客导出工具 Mac By Swift
  10. Jquery 类似新浪微博,鼠标移到头像,用浮动窗口显示用户信息,已做成一个jquery插件
  11. 创建 vxlan 并部署 instance - 每天5分钟玩转 OpenStack(147)
  12. 【转载】小tips: PC端传统网页试试使用Zepto.js进行开发
  13. LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
  14. freemarker常用值格式化方法
  15. Photoshop给人像加上个性裂纹肌肤
  16. 合并多个Excel
  17. Kettle实现数据抽取、转换、装入和加载数据-数据转移ETL工具
  18. 前端大神讲解,初级程序与高级程序写表格变色的区别,dom 0 与dom 1
  19. gitlab数据迁移
  20. Office word 2007不能另存为pdf格式的解决方法

热门文章

  1. Hibernate的注释该如何使用?每一个注释代表什么意思?
  2. mySql 查询当天、本周、最近7天、本月、最近30天的语句
  3. 行为型设计模式之中介者模式(Mediator)
  4. MFC下CSocket编程详解(转)
  5. Andriod布局
  6. WIN8下笔记本共享热点的设置
  7. 阿里云服务器上使用apt-get install出现404 Not Found
  8. jmeter压测脚本编写与静态文件处理
  9. hdu 1598(最小生成树)
  10. git 本地与远程关联流程