【问题标题】:Ant else condition executionAnt else 条件执行
【发布时间】:2016-09-07 05:53:37
【问题描述】:

我正在尝试根据某些条件在 Ant 中打印一些消息,如下所示:

<if>
    <equals arg1="${docs.present}" arg2="true"/>
    <then>
    </then>
    <else>
        <echo message="No docss found!"/>
    </else>
</if>

但如您所见,如果 docs.present 属性设置为“true”,那么我只想执行 else 部分。 if 部分没有什么可以执行的。我怎样才能做到这一点?

【问题讨论】:

    标签: java ant ant-contrib


    【解决方案1】:

    您可以在 if 条件中使用 echo 来代替 else,如下所示:

    <if>
        <equals arg1="${docs.present}" arg2="false"/>
        <then>
              <echo message="No docss found!"/>
        </then>
    </if>
    

    【讨论】:

    • 条件是,如果属性未设置为 true,则打印。你的做法正好相反。
    • @s对不起,我忘了替换条件值。希望它对你有用
    【解决方案2】:

    下面是在ant中写if、else if和else条件的典型例子。

    <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>
    

    在您的情况下,您可以在 then 标记本身内执行任务,如果您在那里没有要执行的操作,请删除 else 标记。

    <if>
        <equals arg1="${docs.present}" arg2="false"/>
        <then>
            <echo message="No docss found!"/>
        </then>
    </if>
    

    【讨论】:

      【解决方案3】:

      原生 Ant 解决方案是使用条件任务执行:

      <project name="demo" default="run">
      
         <available file="mydoc.txt" property="doc.present"/>
      
         <target name="run" unless="doc.present">
           <echo message="No docs found"/>
         </target>
      
      </project>
      

      “if”任务不是标准 Ant 的一部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 2022-11-14
        • 2017-11-12
        • 2011-09-17
        • 1970-01-01
        相关资源
        最近更新 更多