简体   繁体   English

获取条件不支持嵌套的“then”元素<condition></condition>

[英]Getting condition doesn't support the nested "then" element in <condition>

I'm new to Ant/Apache.我是 Ant/Apache 的新手。 When I tried to use <condition> tag in XML it's throwing an error.当我尝试在 XML 中使用<condition>标签时,它抛出了一个错误。 condition doesn't support the nested "then" element.条件不支持嵌套的“then”元素。 Here is my code这是我的代码

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

I've tried with IF also but since my Ant version is not supporting to do this.我也尝试过IF但因为我的 Ant 版本不支持这样做。 Can someone help to resolve this issue.有人可以帮助解决这个问题。 Thanks!谢谢! in advance提前

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

Explanation: Calling way "ant -Dcopy.old = true prepare-copy".解释:调用方式“ant -Dcopy.old = true prepare-copy”。 Here we are passing to copy old file hence, "Old File Copied" will copied.在这里,我们正在传递以复制旧文件,因此将复制“旧文件已复制”。 If you call it like "ant prepare-copy" it'll call "Default File Coping".如果你称它为“ant prepare-copy”,它会调用“Default File Coping”。

Kindly Accept my answer if it is answered your question.Thankyou!如果回答了您的问题,请采纳我的回答。谢谢!

The condition task simply sets a property; condition任务只是设置一个属性; it doesn't contain nested build logic.它不包含嵌套构建逻辑。 The property that it sets can later be used to control which targets are executed.它设置的属性稍后可用于控制执行哪些目标。

While you can use antcontrib's extra if , then , and else tasks to accomplish something like what you showed in your example, I'd recommend sticking to the native Ant approach, which relies on target dependencies and uses separate targets to control build logic:虽然您可以使用 antcontrib 的额外ifthenelse任务来完成您在示例中展示的内容,但我建议坚持使用本机 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>

With the above script, you would run ant build (possibly with -DApplicationName=old ).使用上面的脚本,您将运行ant build (可能使用-DApplicationName=old )。 The build target depends on both copy targets, both of which depend on determine-ae-build . build目标取决于两个copy目标,这两个目标都取决于determine-ae-build The determine-ae-build target will therefore run first.因此determine-ae-build目标将首先运行。 If ApplicationName is set to "old" (either from a properties file that has been loaded, or from being provided in command line with -DApplicationName=old ) then the property copy.old will be set to true.如果ApplicationName设置为“old”(来自已加载的属性文件,或在命令行中使用-DApplicationName=old提供),则属性copy.old将设置为 true。 Otherwise it will remain unset.否则它将保持未设置状态。

Then copy-3.8 and copy-3.9 will be called.然后将调用copy-3.8copy-3.9 If copy.old is is true , copy-3.8 will run.如果copy.oldtruecopy-3.8将运行。 Otherwise, it will be skipped.否则,它将被跳过。 copy-3.9 has no condition so it will run no matter what. copy-3.9没有条件,所以无论如何它都会运行。

Lastly, the build target will execute because it was the original target called from the command line, but it contains no actual steps so the build will finish.最后, build目标将执行,因为它是从命令行调用的原始目标,但它不包含任何实际步骤,因此构建将完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM