2009-10-24 13 views
13

Próbuję przypisać dwa różne ciągi do dwóch różnych zmiennych w zależności od dwóch wartości logicznych w Ant.Ant (1.6.5) - Jak ustawić dwie właściwości w jednym <condition> lub <if>

Pseudokod (owski):

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

Co Próbowałem to;

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

ale pojawia wyjątku null pointer do wiersza zawierającego "<if>". Mogę go uruchomić przy użyciu tagów <condition property=...>, ale mogę ustawić tylko jedną właściwość naraz. Próbowałem używać <propertyset>, ale to też nie było dozwolone.

Jestem nowy dla mrówki, jak się pewnie domyślasz :).

Gav

Odpowiedz

34

Istnieje kilka sposobów, aby to zrobić. Najprostszym jest po prostu użyć dwóch condition sprawozdań, a także skorzystać z właściwości niezmienności:

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

Jest to nieco kłopotliwe, a nie skala dobrze, ale tylko przez dwa właściwości Prawdopodobnie korzystać z tego podejścia.

Nieco lepszym sposobem jest użycie cel Warunkowo:

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

Jesteśmy ponownie wykorzystując właściwości niezmienności tutaj. Jeśli nie chcesz, aby to zrobić, można użyć atrybutu unless oraz dodatkowy poziom zadnie:

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

Ważne jest, aby pamiętać, że if i unless atrybuty target tylko sprawdzić, czy nieruchomość jest ustawić, a nie wartość właściwości.

+2

Dzięki, wyczerpująca odpowiedź. – gav

+0

Właśnie tego potrzebowałem. Dzięki za posiadanie Mózgu na Mrówce, w '09. –