[英]Getting condition doesn't support the nested "then" element in <condition>
我是 Ant/Apache 的新手。 当我尝试在 XML 中使用<condition>
标签时,它抛出了一个错误。 条件不支持嵌套的“then”元素。 这是我的代码
<target name="determine-ae-build">
<condition property="ApplicationName">
<equals arg1="${ApplicationName}" arg2="new"/>
<then>
<echo>3.9 Robots Config Copied</echo>
</then>
<else>
<condition property="ApplicationName">
<equals arg1="${ApplicationName}" arg2="old"/>
<then>
<echo>3.8 Robots Config Copied</echo>
</then>
<else>
<echo>3.9 Robots Config Copied</echo>
</else>
</condition>
</else>
</condition>
</target>
我也尝试过IF
但因为我的 Ant 版本不支持这样做。 有人可以帮助解决这个问题。 谢谢! 提前
<target name="prepare-copy" description="copy file based on condition" depends="determine-ae-build, prepare-copy-old, prepare-copy-new, prepare-copy-default">
<sleep seconds="10"/> --To read the results
</target>
<target name="prepare-copy-old" description="copy file based on condition" if="copy.old">
<echo>Old File Copied </echo>
</target>
<target name="prepare-copy-new" description="copy file based on condition" if="copy.new">
<echo>New File Copied</echo>
</target>
<target name="prepare-copy-default" description="copy file based on false condition" if="copy.default">
<echo>Default File Coping</echo>
</target>
<target name="determine-ae-build">
<condition property="copy.old">
<equals arg1="${ApplicationName}" arg2="old"/>
</condition>
<condition property="copy.new">
<equals arg1="${ApplicationName}" arg2="new"/>
</condition>
<condition property="copy.default">
<not>
<or>
<equals arg1="${ApplicationName}" arg2="new"/>
<equals arg1="${ApplicationName}" arg2="old"/>
</or>
</not>
</condition>
</target>
解释:调用方式“ant -Dcopy.old = true prepare-copy”。 在这里,我们正在传递以复制旧文件,因此将复制“旧文件已复制”。 如果你称它为“ant prepare-copy”,它会调用“Default File Coping”。
如果回答了您的问题,请采纳我的回答。谢谢!
condition
任务只是设置一个属性; 它不包含嵌套构建逻辑。 它设置的属性稍后可用于控制执行哪些目标。
虽然您可以使用 antcontrib 的额外if
、 then
和else
任务来完成您在示例中展示的内容,但我建议坚持使用本机 Ant 方法,该方法依赖于目标依赖项并使用单独的目标来控制构建逻辑:
<project name="build" basedir="." default="build">
<target name="build" depends="copy-3.8,copy-3.9" />
<target name="copy-3.8" depends="determine-ae-build" if="copy.old">
<echo>3.8 Robots Config Copied</echo>
</target>
<target name="copy-3.9" depends="determine-ae-build">
<echo>3.9 Robots Config Copied</echo>
</target>
<target name="determine-ae-build">
<condition property="copy.old">
<equals arg1="${ApplicationName}" arg2="old"/>
</condition>
</target>
</project>
使用上面的脚本,您将运行ant build
(可能使用-DApplicationName=old
)。 build
目标取决于两个copy
目标,这两个目标都取决于determine-ae-build
。 因此determine-ae-build
目标将首先运行。 如果ApplicationName
设置为“old”(来自已加载的属性文件,或在命令行中使用-DApplicationName=old
提供),则属性copy.old
将设置为 true。 否则它将保持未设置状态。
然后将调用copy-3.8
和copy-3.9
。 如果copy.old
为true
, copy-3.8
将运行。 否则,它将被跳过。 copy-3.9
没有条件,所以无论如何它都会运行。
最后, build
目标将执行,因为它是从命令行调用的原始目标,但它不包含任何实际步骤,因此构建将完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.