2009-10-24 27 views
13

'da iki özellik nasıl ayarlanır? Ant içerisindeki iki boole bağımlı olarak iki farklı değişkene iki farklı dize atamaya çalışıyorum.Ant (1.6.5) - Bir <condition> veya <if>

Sözdekod (imsi): denedim ne

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

olduğu;

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

Ama "<if>" içeren hat için bir boş gösterici istisna olsun. <condition property=...> etiketlerini kullanarak çalışmaya başlayabilirim ancak bir seferde yalnızca bir özellik belirleyebilir. <propertyset> kullanmayı denedim, ancak buna da izin verilmedi.

Muhtemelen tahmin edeceğiniz gibi karıncaya yeniyim :).

cevap

34

Bunu yapmanın birkaç yolu vardır.

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

Bu biraz hantal ve iyi ölçek değildir, ama sadece iki özellik için ben muhtemelen bu yaklaşımı kullanır: en basit sadece iki condition ifadeleri kullanın ve mülkiyet değişmezlik istifade etmektir.

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

Biz burada tekrar mülkiyet değişmezlik yararlanmaktadır:

bir miktar daha iyi bir yol koşullu bir hedef kullanmaktır. bunu yapmak istemiyorsanız, size unless niteliğini ve fazladan bir seviyede kullanabilirsiniz:

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

özellik ise target ait if ve unless nitelikleri sadece çek olduğuna dikkat etmek önemlidir Set, mülkün değerini değil.

+2

Teşekkürler, kapsamlı yanıt. – gav

+0

Sadece ihtiyacım olan şey. '09 'da tekrar Antrenmanınız için teşekkürler. –

1

Ant-Contrib kütüphanesini temiz bir <if><then><else> sözdizimine sahip olmak için kullanabilirsiniz, ancak birkaç indirme/yükleme gerektirecektir.

Diğer SO sorularına bakın: ant-contrib - if/then/else task

İlgili konular