听风的歌

欣赏永运比跟风好

◎Ant的使用 - 常用任务标签(一)

建立一个基本的程序通常有几个常用的任务,像是设置通用属性、创建或删除目录、编译程序、打包、拷贝文件等等。
这些任务,我们都可以在build.xml中编写相应的任务目标[target]

■ 属性设定:<property>

如果一个固定的值(如:目录D:\dev\lib),在多个target中被应用到,这时我们就通过<property>对它设定,应用到它的地方都使用设定的<property>代替,如果日后有发生变化,也只需要改动<property>的设定即可

<target name="defProperty" description="设定属性">
      <property name="tomcat.home" value="C:/jakarta-tomcat" />
     
<property name="app.home"  value="." />
     
<property name="src.home" value="${app.home}/src"/>
     
<property name="lib.home" value="${app.home}/WEB-INF/lib"/>
     
<property name="classes.home" value="${app.home}/WEB-INF/classes"/>
</target>

每个property都有一个name(名称)和一个value(值),要引用某个property可以使用${property-name},像上面的${app.home}。
这些属性的值也可以在执行的时候指定,如:ant -Dapp.home="d:\dev\book",如果执行时没有指定,那么就会使用value上定义的值。


■ 创建目录:<mkdir>
<target name="createDir" description="创建必要的目录">
    
<mkdir dir="test/classes">
    
<mkdir dir="${DIST.DIR}"/>
</target>
如果父目录不存在,也会被同时创建;如test不存在,会先创建test目录,然后在其下创建classes目录。可以看到我们这里使用了一个属性DIST.DIR代替一个具体的目录。

■ 创建目录和文件:<delete>
<target name="delDirAndFile" description="删除目录和文件">
    
<!-- 删除目录 -->
    
<delete dir="${classes.dir}"/>
    
<!-- 删除指定的文件 -->
    
<delete file="${jar.file}"/>
    
<!-- 删除指定目录的一组文件 -->
    
<delete>
       
<fileset dir="${classes.dir}" includes="**/*.class"/>
    
</delete>
    
<!-- 删除指定目录及子目录,同时也删除它自己 -->
    
<delete includeEmptyDirs="true">
       
<fileset dir="build"/>
    
</delete>
</target>

■ 拷贝文件和目录:<copy>
<target name="copyFileAndDir" description="拷贝文件和目录">
    
<!-- 拷贝单个文件 -->
    
<copy file="error.log" tofile="debug.log"/>
    
    
<!-- 拷贝单个文件到指定目录 -->
    
<copy file="error.log" todir="${LOG.DIR}"/>

    
<!-- 拷贝多个文件到指定目录 -->
    
<copy todir="${LOG.DIR}">
        
<fileset dir="log">
            
<include name="**/*.log"/>
            
<exclude name="**/error.log"/>
        
</fileset>
    
</copy>
    
<!-- 同上 -->
    
<!--
    <copy todir="${LOG.DIR}">
        <fileset dir="log" excludes="**/*.txt"/>
    </copy>
    
-->

    
<!-- 拷贝一个目录到另一个目录 -->
    
<copy todir="newbin">
       
<fileset dir="bin"/>
    
</copy>
</target>

■ 移动或重命名文件、目录:<move>
<target name="moveFileAndDir" description="移动或重命名文件和目录">
    
<!-- 移动或重命名一个文件 -->
    
<move file="error.log" tofile="debug.log"/>

    
<!-- 移动或重命名一个文件到另一个目录下 -->
    
<move file="error.log" todir="new/log"/>

    
<!-- 将目录移到另一个目录下 -->
    
<move todir="new/log">
        
<fileset dir="log"/>
    
</move>

    
<!-- 一组文件移到另一个目录下 -->
    
<move todir="new/log">
        
<fileset dir="log">
            
<include name="**/*.log"/>
            
<exclude name="**/error.log"/>
        
</fileset>
    
</move>  
</target>

posted on 2007-04-10 15:41 听风的歌 阅读(1273) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2007年4月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜