打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ant 中 if else - 学习笔记

ant中的条件判断实例:

ant中条件判断这里有2种形式,一种是运用 target 的if and unless attributes,一种是运用ant-contrib中的if else。

第一种:
<project name="test" basedir="." default="">
    <condition property="test.exist">
        <and>
            <available file="test-1.0.jar" filepath="test/target/>
        </and>
    </condition>
    <target name="copy-target" if="test.exist" description="Test Copy">
        <copy todir="test/libdb" preservelastmodified="true">
            <fileset dir="test/target">
                <include name="test-1.0.jar"/>
            </fileset>
        </copy>
    </target>
    <target name="copy" unless="test.exist" depends="copy-target">
        <copy todir="test/libdb" preservelastmodified="true">
            <fileset dir="test/built">
                <include name="test-1.0.jar"/>
            </fileset>
        </copy>
    </target>
</project>
如果test/target中test-1.0.jar存在,就把它copy到test/libdb目录下。
如果不存在就从test/built中把test-1.0.jar copy到test/libdb目录下。

第二种:
1.先到http://ant-contrib.sourceforge.net/网站下载最新的ant-contrib.jar;
  1.1 copy ant-contrib.jar到ant安装目录下的lib目录下,如果你想在你的工程中用这个if-else的tasks,就添加下面一行到你的 build.xml文件中:
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

  1.2 也可以把ant-contrib.jar copy到一个相对独立的目录下,但是你在用的时候一定要指定这个目录,以便于ant能找到它,例如(lib 目录D:/ant-contrib),code如下:

<project name="test" basedir="." default="">
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="D:/ant-contrib/ant-contrib-1.0b2.jar"/>
        </classpath>
    </taskdef>
    <available property="test.exist" file="test-1.0.jar" filepath="test/target"/>
    <target name="copy" description="Test Copy">
        <if>
            <isset property="test.exist"/>
            <then>
                <copy todir="test/libdb" preservelastmodified="true">
                    <fileset dir="test/target">
                        <include name="test-1.0.jar"/>
                    </fileset>
                </copy>
            </then>
            <else>
                <copy todir="test/libdb" preservelastmodified="true">
                    <fileset dir="test/built">
                        <include name="test-1.0.jar"/>
                    </fileset>
                </copy>
            </else>
        </if>
    </target>
</project>

2. available 释意:
Available判断某个类,或某个文件,或某个路径。如果存在,则设置某个property。返回true.
其格式如下:
    判断某个类是否存在:
    <available property="class.exist" classname="package.test" classpath ="dist/test.jar"/>
    判断某个文件是否存在:
    <available property="file.exist" file="test.txt" filepath="src/test" type= "file"/>
    判断某个资源是否存在:
    <available property="resource.exist" resource="package/test/test1.class" classpath="dist/test.jar"/>

3. ant-contrib参考地址:

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ant-contrib 扩展ant 功能 for、foreach、if
怎样用Ant测试Spring项目(Ant测试自动化--下
Hadoop2.4.0 Eclipse插件制作
hadoop eclipse plugin 编译安装,问题总结
Eclipse中使用ANT
Sun技术社区 - Java,Solaris,SunONE,JES,StarSuite技术论坛 - Ant应用2
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服