You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ant中<condition>标签不支持嵌套<then>元素,求低版本适配方案

问题解决:Ant 不支持嵌套 元素的报错

问题原因

Ant的<condition>标签核心作用是根据条件设置属性,它本身并不支持嵌套<then>和<else>元素来执行任务逻辑,这就是你触发condition doesn't support the nested "then" element报错的原因。

解决方案(兼容低版本Ant)

由于你的Ant版本不支持<if>标签,我们可以用Ant原生的target条件属性(if/unless)来实现分支逻辑,步骤如下:

  1. 先用<condition>分别判断不同的版本场景,设置对应的标记属性
  2. 创建不同的target,通过if/unless属性控制是否执行
  3. 在主target中调用这些分支target

修正后的代码示例

<target name="determine-ae-build">
    <!-- 判断是否为new版本,设置标记属性 -->
    <condition property="is.new.version">
        <equals arg1="${ApplicationName}" arg2="new"/>
    </condition>
    <!-- 判断是否为old版本,设置标记属性 -->
    <condition property="is.old.version">
        <equals arg1="${ApplicationName}" arg2="old"/>
    </condition>

    <!-- 调用对应分支任务 -->
    <antcall target="handle-new-version"/>
    <antcall target="handle-old-version"/>
    <antcall target="handle-default-version"/>
</target>

<!-- 处理new版本逻辑 -->
<target name="handle-new-version" if="is.new.version">
    <echo>3.9 Robots Config Copied</echo>
</target>

<!-- 处理old版本逻辑 -->
<target name="handle-old-version" if="is.old.version">
    <echo>3.8 Robots Config Copied</echo>
</target>

<!-- 处理默认情况(非new/old版本) -->
<target name="handle-default-version" unless="is.new.version" unless="is.old.version">
    <echo>3.9 Robots Config Copied</echo>
</target>

代码说明

  • <condition>仅负责设置标记属性,不包含任务逻辑
  • 每个分支逻辑单独放在一个target中,通过if(属性存在则执行)或unless(属性不存在则执行)控制触发
  • 主target通过<antcall>调用所有分支target,Ant会自动根据标记属性判断是否执行对应分支

内容的提问来源于stack exchange,提问作者Chanikya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 20:20:48