【问题标题】:Ant if else condition?蚂蚁 if else 条件?
【发布时间】:2013-01-24 11:43:58
【问题描述】:

是否有可用于 Ant 任务的 if/else 条件?

这是我目前所写的:

<target name="prepare-copy" description="copy file based on condition">
        <echo>Get file based on condition</echo>
    <copy file="${some.dir}/true" todir="."  if="true"/>
</target>

如果条件为真,上面的脚本将复制文件。如果条件为假并且我想复制另一个文件怎么办?这在 Ant 中可能吗?

我可以将参数传递给上述任务并确保传递的参数是

【问题讨论】:

    标签: ant


    【解决方案1】:

    &lt;copy&gt; 不存在 if 属性。它应该应用于&lt;target&gt;

    下面是一个示例,说明如何使用目标的depends 属性和if and unless attributes 来控制相关目标的执行。两者中只有一个应该执行。

    <target name="prepare-copy" description="copy file based on condition"
        depends="prepare-copy-true, prepare-copy-false">
    </target>
    
    <target name="prepare-copy-true" description="copy file based on condition"
        if="copy-condition">
        <echo>Get file based on condition being true</echo>
        <copy file="${some.dir}/true" todir="." />
    </target>
    
    <target name="prepare-copy-false" description="copy file based on false condition" 
        unless="copy-condition">
        <echo>Get file based on condition being false</echo>
        <copy file="${some.dir}/false" todir="." />
    </target>
    

    如果您使用的是 ANT 1.8+,那么您可以使用属性扩展,它将评估属性的值以确定布尔值。因此,您可以使用if="${copy-condition}" 而不是if="copy-condition"

    在 ANT 1.7.1 和更早版本中,您指定属性的名称。如果该属性已定义并且具有任何值(甚至是空字符串),那么它将评估为 true。

    【讨论】:

    • 在这个例子中,是否有可能在“prepare-copy”下包含一些元素,无论使用“prepare-copy-true”还是“prepare-copy-false”都可以使用?
    • 当然。目标可以有内容,任务会在依赖目标之后执行。
    【解决方案2】:

    您也可以使用ant contrib's if task 执行此操作。

    <if>
        <equals arg1="${condition}" arg2="true"/>
        <then>
            <copy file="${some.dir}/file" todir="${another.dir}"/>
        </then>
        <elseif>
            <equals arg1="${condition}" arg2="false"/>
            <then>
                <copy file="${some.dir}/differentFile" todir="${another.dir}"/>
            </then>
        </elseif>
        <else>
            <echo message="Condition was neither true nor false"/>
        </else>
    </if>
    

    【讨论】:

    • 另外需要注意的是,ant contrib不再维护。最后一个版本是 2008 年的!
    • ant-contrib 的最新版本 1.0b3 提供 if/else、for、switch 等功能,日期为 2006 年 11 月 2 日! cpptasks-1.0-beta5 的日期为 2008-04-03。见sourceforge.net/projects/ant-contrib/files/ant-contrib
    • 旧的并不意味着它没有任何好处或仍然不能工作:)
    【解决方案3】:

    在目标上使用条件的古怪语法(由 Mads 描述)是在核心 ANT 中执行条件执行的唯一受支持的方式。

    ANT 不是一种编程语言,当事情变得复杂时,我选择在我的构建中嵌入一个脚本,如下所示:

    <target name="prepare-copy" description="copy file based on condition">
        <groovy>
            if (properties["some.condition"] == "true") {
                ant.copy(file:"${properties["some.dir"]}/true", todir:".")
            }
        </groovy>
    </target>
    

    ANT 支持多种语言(参见script 任务),我更喜欢Groovy,因为它的语法简洁,而且它在构建过程中表现得非常好。

    抱歉,David 我不是ant-contrib 的粉丝。

    【讨论】:

    • 不确定如何让它工作 - 似乎没有 groovy 标签。 failed to create task or type groovy.
    【解决方案4】:

    从 ant 1.9.1 开始,您可以使用 if:set 条件:https://ant.apache.org/manual/ifunless.html

    【讨论】:

      【解决方案5】:
      <project name="Build" basedir="." default="clean">
          <property name="default.build.type" value ="Release"/>
      
          <target name="clean">
          <echo>Value Buld is now  ${PARAM_BUILD_TYPE} is set</echo>
              <condition property="build.type" value="${PARAM_BUILD_TYPE}" else="${default.build.type}">
                  <isset property="PARAM_BUILD_TYPE"/>
              </condition>
      
             <echo>Value Buld is now  ${PARAM_BUILD_TYPE} is set</echo>
             <echo>Value Buld is now  ${build.type} is set</echo>
          </target>
      </project>
      

      在我的案例DPARAM_BUILD_TYPE=Debug 中,如果提供了它,我需要为 Debug 构建,否则我需要构建 Release 构建。 我像上面写的那样工作,我已经测试过它对我来说工作正常。

      并且属性${build.type} 我们可以将其传递给其他目标或宏定义,以便我在我的其他蚂蚁宏定义中进行处理。

      D:\>ant -DPARAM_BUILD_TYPE=Debug
      Buildfile: D:\build.xml
      clean:
           [echo] Value Buld is now  Debug is set
           [echo] Value Buld is now  Debug is set
           [echo] Value Buld is now  Debug is set
      main:
      BUILD SUCCESSFUL
      Total time: 0 seconds
      D:\>ant
      Buildfile: D:\build.xml
      clean:
           [echo] Value Buld is now  ${PARAM_BUILD_TYPE} is set
           [echo] Value Buld is now  ${PARAM_BUILD_TYPE} is set
           [echo] Value Buld is now  Release is set
      main:
      BUILD SUCCESSFUL
      Total time: 0 seconds
      

      实现条件对我有用,因此发布希望它会有所帮助。

      【讨论】:

        猜你喜欢
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-23
        • 2015-03-22
        • 2010-10-28
        • 1970-01-01
        • 2010-09-29
        相关资源
        最近更新 更多