继续学习Ant的一些task属性

 

5. 复制文件与目录copy

直接转载别人写好的 http://www.blogjava.net/hh-lux/archive/2009/04/07/86635.html

① 拷贝单个文件到指定目录下
例:<copy todir="${basedir}/new" file="${basedir}/old/old1.txt1"> 
将${basedir}/old/old.txt文件拷贝到${basedir}/new下

② 拷贝一批文件到指定目录下
例:

<copy todir="${basedir}/new">
           <fileset dir="${basedir}/old">
              <include name="old1.txt" />
              <include name="old2.txt" />
              <exclude name="old8.txt" />
          </fileset>
       </copy>


      这里fileset定义的是原文件的组成形式,<include/>子属性表示包括,<exclude/>子属性表示排除,很简单,通过他们组合实现多文件的筛选,当然我这个例子用得很傻。比如
         

<include name="appgen/**"/>
                <include name="ibatis/**"/>
                <exclude name="**/*.log"/>


      拷贝appget目录和ibatis目录下除了.log文件以外的其它所有文件和子目录。

      可以把<fileset/>简写成<fileset dir="${basedir}/old" includes="old1.txt,old2.txt" />,includes可以理解成include的复数形式,包含多个文件时用逗号隔开,excludes也一样。

③ 拷贝一个目录到指定目录下
例:

<copy todir="${basedir}/new">
           <fileset dir="${basedir}/old">
             <include name="appgen" />
             <include name="appgen/" />
             <include name=appgen/**" />
             <include name="appgen/***" />
           </fileset>
       </copy>


      同样使用<fileset/>属性,name指定目录名,不过这里要分两种情况,用<include/>子属性和不用<include/>子属性.
若使用<include/>, 又要分三种情况
          若是“appgen”,则只会拷贝名为appgen的空目录过去,它里面的文件和子目录则不会拷贝。
          若是“appgen/”,或“appgen/**”,则会把整个appgen目录拷贝过去,包括里面的文件和子目录。
          若是“appgen/*”,则只会把该目录和该目录下第一级子目录的所有东西拷贝过去,而不会拷贝第二级和第二级以下的。注:“appgen/*”这儿是一个*号,*号若大于两个,也跟一个*号是同样效果。比如“appgen/*”和“appgen/****”都只拷贝appgen目录下第一级子目录。

注:若appeng这个目录本身就是个空目录,无论怎么写,这个空目录都不会被拷贝。也就是说,copy操作不会产生创建空目录的作用,要想创建空目录,只有用mkdir。

      若不使用任何<include>属性,如
  

<fileset dir="${basedir}/old">
           </fileset>


会拷贝${basedir}/old下的所有文件和子目录。

注:使用<exclude/>排除目录时,目录名必须写成“appgen/”或“appgen/**”形式,否则不会生效。

以上是三种拷贝到目录的种类,注意如果计算机中没有todir指定的路径,ant将会自动创建这个路径。

④ 拷贝单个的文件: 
〈copy tofile="old.txt" file="new.txt" /〉就这么简单就行了。

6. 删除文件和目录delete

(1) 删除一个文件

<delete file="../.."/>

(2) 删除一个目录

<delete dir="lib"/>

(3) 删除一个目录下符合规则的文件

<delete>
    <fileset dir="." includes="**/.jar"/>
  </delete>

(4) 可以添加一个verbose,会显示所有删除的文件的名称

 

7. 剪贴文件和目录move

和copy语法相同,把copy换成move即可

 

8.重命名rename

<rename src="" dest=""/>

 

9. Condition元素的使用

* <condition>任务是在某一个判断条件为true的时候设置一个属性
* 这是<available>, <uptodate>和其它类似任务的一个泛化,或者说是一个组成
* 如果条件为真,那么要设置的属性值被设置为默认值
* 否则的话,属性完全不被设置(作为undefined出现)。
* 你可以自行制定value属性而不是采用默认的。
* 判断条件 通过一个嵌套的<condition>任务来制定。

antdesign 一键复制 ant 复制目录_子目录

 

 10. available的使用

Available用于判断某个文件/目录是否存在,根据结果决定某个属性是true还是false,用于运行时的决策

available主要判断3类文件:class,file和dir

antdesign 一键复制 ant 复制目录_antdesign 一键复制_02

 如上图所示,第一个available判断一个class文件是否存在,如果存在则设定HelloAnt.Present为true,如果不存在或者找不到,则打印的时候直接为${HelloAnt.Present}

 第二个available判断文件juven是否存在

 第三个available判断目录output是否存在,注意使用了tpye参数指定类型为目录

 

需要注意的地方:

① 对于判断jar和class文件,可以指定classpath,但这时候要注意包机制。pathelement指定的path应该包所在的目录,而不是class文件所在的目录,例如:

 java文件包路径为org.apache.hadoop,文件名为namenode,绝对路径是/usr/hadoop/org/apache/hadoop.namenode

   那么classpath中的地址写:/usr/hadoop

 classname写为org.apache.hadoop.namenode(不带.class)

② 对于查找的是文件和目录的,必须给出合理的路径(比如绝对路径)而不能使用classpath指定路径

③ 常用的available参数

Attribute

Description


Required

property

The name of the property to set.

Yes

value

The value to set the property to. Defaults to "true".

No

classname

The class to look for in the classpath.

Yes

file

The file to look for.

resource

The resource to look for in the JVM.

classpath

The classpath to use when looking up classname or resource.

No

filepath

The path to use when looking up file.

No

classpathref

The classpath to use, given as a reference to a path defined elsewhere.

No

type

The type of file to look for, either a directory (type="dir") or a file (type="file"). If not set, the property will be set if the name specified in the file attribute exists as either a file or a directory.

No

ignoresystemclasses

Ignore Ant's runtime classes, using only the specified classpath. Only affects the "classname" attribute. Defaults to "false"

No

searchparents

This contains the behaviour of the "file" type. If true, the available task will, when searching for a file, search not only the directories specified but will also search the parent directories of those specified. If false, only the directories specified will be searched. Defaults to "false". Since Ant 1.7

No