Ant else 条件执行

Ant else condition execution

我正在尝试根据以下条件在 Ant 中打印一些消息:

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

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

您可以在 if 条件中使用 echo 而不是 else,如下所示:

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

下面是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>

原生 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 的一部分。