Ant(1.6.5)-1つの< condition>に2つのプロパティを設定する方法または< if>

StackOverflow https://stackoverflow.com/questions/1618493

質問

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>

しかし、&quot; &lt; if&gt; &quot;を含む行に対してnullポインタ例外が発生します。 &lt; condition property = ...&gt; タグを使用して動作させることができますが、一度に1つのプロパティしか設定できません。 &lt; propertyset&gt; を使用しようとしましたが、それも許可されませんでした。

おそらく推測するかもしれませんが、私はantが初めてです:)。

Gav

役に立ちましたか?

解決

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

target if および unless 属性は、プロパティの値のみではなく、プロパティが設定されているかどうかのみを確認することが重要です。プロパティ。

他のヒント

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

SOの他の質問をご覧ください: ant-contrib-if / then / else task

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top