【问题标题】:Ant (1.6.5) - How to set two properties in one <condition> or <if>Ant (1.6.5) - 如何在一个 <condition> 或 <if> 中设置两个属性
【发布时间】:2010-12-09 18:18:42
【问题描述】:

我正在尝试将两个不同的字符串分配给两个不同的变量,这取决于 Ant 中的两个布尔值。

伪代码(ish):

if(condition)
   if(property1 == null)
      property2 = string1;
      property3 = string2;
   else
      property2 = string2;
      property3 = string1;

我试过的是;

<if>
  <and>
    <not><isset property="property1"/></not>
    <istrue value="${condition}" />
  </and>
  <then>
    <property name="property2" value="string1" />
    <property name="property3" value="string2" />
  </then>
  <else>
    <property name="property2" value="string2" />
    <property name="property3" value="string1" />
  </else>
</if>

但我得到包含“&lt;if&gt;”的行的空指针异常。我可以使用&lt;condition property=...&gt; 标签让它工作,但一次只能设置一个属性。我尝试使用&lt;propertyset&gt;,但这也是不允许的。

你可能已经猜到了,我是 ant 新手 :)。

Gav

【问题讨论】:

    标签: ant properties conditional-statements if-statement


    【解决方案1】:

    有几种方法可以做到这一点。最直接的方法是只使用两个condition 语句,并利用属性不变性:

    <condition property="property2" value="string1">
        <isset property="property1"/>
    </condition>
    <condition property="property3" value="string2">
        <isset property="property1"/>
    </condition>
    
    <!-- Properties in ant are immutable, so the following assignments will only
         take place if property1 is *not* set. -->
    <property name="property2" value="string2"/>
    <property name="property3" value="string1"/>
    

    这有点麻烦并且不能很好地扩展,但是对于两个属性我可能会使用这种方法。

    更好的方法是使用条件目标:

    <target name="setProps" if="property1">
        <property name="property2" value="string1"/>
        <property name="property3" value="string2"/>
    </target>
    
    <target name="init" depends="setProps">
        <!-- Properties in ant are immutable, so the following assignments will only
             take place if property1 is *not* set. -->
        <property name="property2" value="string2"/>
        <property name="property3" value="string1"/>
    
        <!-- Other init code -->
    </target>
    

    我们在这里再次利用了属性不变性。如果您不想这样做,可以使用unless 属性和额外的间接级别:

    <target name="-set-props-if-set" if="property1">
        <property name="property2" value="string1"/>
        <property name="property3" value="string2"/>
    </target>
    
    <target name="-set-props-if-not-set" unless="property1">
        <property name="property2" value="string2"/>
        <property name="property3" value="string1"/>
    </target>
    
    <target name="setProps" depends="-set-props-if-set, -set-props-if-not-set"/>
    
    <target name="init" depends="setProps">
        <!-- Other init code -->
    </target>
    

    需要注意的是targetifunless属性只检查是否设置了属性,而不检查属性的值。

    【讨论】:

    • 正是我所需要的。感谢您在 09 年将您的大脑放在 Ant 上。
    【解决方案2】:

    您可以使用Ant-Contrib 库来访问简洁的&lt;if&gt;&lt;then&gt;&lt;else&gt; 语法,但它需要一些下载/安装步骤。

    查看其他 SO 问题:ant-contrib - if/then/else task

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多