ant脚本的一些使用

来源:互联网 发布:徐州管家婆软件 编辑:程序博客网 时间:2024/06/12 13:43

参考:

http://blog.csdn.net/yieryi_/article/details/48877677

http://blog.csdn.net/caolaosanahnu/article/details/7886290


1.macrodef

ant脚本的宏,macrodef和target平级,target可以调用macrodef,而macrodef不可以调用target。

用法:

<macrodef name="hong">

    <attribute name="bianliang"/>

    <attribute name="bianliang2"/>

    <echo message="@{bianliang}"/>

    <echo message="@{bianliang2}"/>

</macrodef>

其中attribute定义宏中使用的变量,使用的时候用@,而不是$,在target中调用如下。

<target name="diaoyong">

    <hong bianliang="11" bianliang2="22"/>

</target>

其中target可以通过宏的名字直接调用,通过attribute中的name来设定其值。

2.判断是否存在

Available判断某个类、文件、路径是否存在

1)判断某个类是否存在

<available property="class.exist" classname="package.test" classpath="dist/test.jar"/>

2)判断某个文件是否存在

<available property="file.exist" file="test.txt" filepath="src/test" type="file"/>

3)判断某个目录是否存在

<available property="file.exist" file="dirname" filepath="src/test" type="dir"/>

4)判断某个资源是否存在

<available property="resource.exist" resource="package/test/test.class" classpath="dist/test.jar"/>

3.判断文件是否存在

需要在ant的lib包中引入ant-contrib.jar

使用该jar包有两种方式

1)

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

2)把ant-contrib.jar copy到一个相对独立的目录下,但是你在用的时候一定要指定这个目录,以便于ant能找到它

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="D:/ant-contrib/ant-contrib-1.0b2.jar"/>
        </classpath>
    </taskdef>


<avaliable property="isExist" file="src/test"/>

<if>

    <isset property="isExist"/>

    <then>

        <copy todir="a1/test" preservelastmodified="true">

            <fileset dir="a2/test"/>

        </copy>

    </then>

    <else>

        <echo message="不存在"/>

    </else>

</if>


4.antcontib参考地址

http://ant-contrib.sourceforge.net/ant-contrib/manual/tasks/index.html