13

Antの2つのブール値に依存する2つの異なる変数に2つの異なる文字列を割り当てようとしています。

擬似コード(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>

しかし、""を含む行に対してnullポインター例外が発生します<if><condition property=...>タグを使用して動作させることはできますが、一度に設定できるプロパティは1つだけです。使ってみ<propertyset>ましたが、それも許されませんでした。

あなたがおそらく推測しているように、私はアリに不慣れです:)。

Gav

4

2 に答える 2

36

これを行うにはいくつかの方法があります。最も簡単なのは、2つの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"/>

これは少し面倒で、適切に拡張できませんが、2つのプロパティについては、おそらくこのアプローチを使用します。

やや良い方法は、条件付きターゲットを使用することです。

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

ifのandunless属性はtarget、プロパティの値ではなく、プロパティが設定されているかどうかのみをチェックすることに注意してください。

于 2009-10-25T19:35:41.880 に答える
1

Ant-Contribライブラリを使用して、きちんとした<if><then><else>構文にアクセスできますが、ダウンロード/インストールの手順がいくつか必要になります。

この他のSOの質問を参照してください:ant-contrib-if / then / else task

于 2012-02-17T11:34:52.057 に答える