简体   繁体   English

蚁巢条件

[英]Ant nested condition

I have an ant build.xml file which contains the following snippet: 我有一个ant build.xml文件,其中包含以下代码段:

<condition property="apiUrl" value="apiUrl1">
  <and>
    <equals arg1="${area}" arg2="area1"/>
    <equals arg1="${env}" arg2="stage"/>
  </and>
</condition>
<condition property="apiUrl" value="apiUrl2">
  <and>
    <equals arg1="${area}" arg2="area1"/>
    <equals arg1="${env}" arg2="develop"/>
  </and>
</condition>

As you can see from above, <equals arg1="${area}" arg2="area1"/> is checked twice, and the logic of the snippet is equivalent to the pseudo code: 从上面可以看到, <equals arg1="${area}" arg2="area1"/>被检查了两次,代码段的逻辑等效于伪代码:

if (${area} == 'area1' and ${env} == 'stage') {
  apiUrl = 'apiUrl1'
}
if (${area} == 'area1' and ${env} == 'develop') {
  apiUrl = 'apiUrl2'
}

How can I change build.xml so that its logic becomes the following nested condition? 如何更改build.xml ,使其逻辑变为以下嵌套条件?

if (${area} == 'area1') {
  if (${env} == 'stage') {
    apiUrl = 'apiUrl1'
  }
  if (${env} == 'develop') {
    apiUrl = 'apiUrl2'
  }      
}

My ant version is 1.10.3 . 我的ant版本是1.10.3

The reason this seemingly minor change can seem so awkward in Ant is because while the conditional setting of properties is simply controlled with the condition task, the conditional flow of logic is controlled at the target level. 在Ant中,这种看似微小的变化看起来如此尴尬的原因是,虽然仅通过condition任务控制属性的条件设置,但逻辑的条件流却控制在target级别。 Thus, if you want certain steps to run or be skipped depending on a condition, you'll have to create a separate target that first checks the condition and then tells your main target whether or not it should run. 因此,如果您希望根据条件运行或跳过某些步骤,则必须创建一个单独的目标,该目标首先检查条件,然后告诉您的主要目标是否应运行。

<target name="setApiUrl" depends="checkArea" if="isArea1">
    <condition property="apiUrl" value="apiUrl1">
        <equals arg1="${env}" arg2="stage"/>
    </condition>

    <condition property="apiUrl" value="apiUrl2">
        <equals arg1="${env}" arg2="develop"/>
    </condition>
</target>

<target name="checkArea">
    <condition property="isArea1">
        <equals arg1="${area}" arg2="area1"/>
    </condition>
</target>

you can achieve that using script instead of condition task like this: 您可以使用script而不是像这样的condition任务来实现:

<project default="init" name="My Project">

    <property name="area" value="area1" />
    <property name="env" value="develop" />

    <target name="init">
        <script language="javascript"> 
            if (project.getProperty('area') == 'area1') {
                if (project.getProperty('env') == 'stage') {
                    project.setProperty('apiUrl', 'apiUrl1');
                }
                if (project.getProperty('env') == 'develop') {
                    project.setProperty('apiUrl', 'apiUrl2');
                }      
            }
        </script>
        <echo>${apiUrl}</echo>
    </target>

</project>

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

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