Java Ant else条件执行
我试图在Ant中打印一些消息,具体取决于以下条件:Java Ant else条件执行,java,ant,ant-contrib,Java,Ant,Ant Contrib,我试图在Ant中打印一些消息,具体取决于以下条件: <if> <equals arg1="${docs.present}" arg2="true"/> <then> </then> <else> <echo message="No docss found!"/> </else> </if> 但正如您所看到的,如果docs.present属性
<if>
<equals arg1="${docs.present}" arg2="true"/>
<then>
</then>
<else>
<echo message="No docss found!"/>
</else>
</if>
但正如您所看到的,如果docs.present属性设置为“true”,那么只有我想只执行else部分。在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>
本机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的一部分。条件是,如果属性未设置为true,则打印。您的方法正好相反。@sAm抱歉,我忘记替换条件值。希望它对你有用
<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>