Domanda

Sto cercando di assegnare due stringhe diverse a due variabili diverse dipendenti da due valori booleani in Ant.

Pseudocodice (ish):

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

Quello che ho provato è;

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

Ma ottengo un'eccezione puntatore null per la riga contenente " < if > " ;. Posso farlo funzionare usando i tag < condition property = ... > ma posso impostare solo una proprietà alla volta. Ho provato a usare < propertyset > ma neanche quello era permesso.

Sono nuovo di formica come probabilmente avrete indovinato :).

Gav

È stato utile?

Soluzione

Esistono diversi modi per farlo. Il più semplice è usare solo due istruzioni condition e sfruttare l'immutabilità della proprietà:

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

Questo è un po 'ingombrante e non si adatta bene, ma per solo due proprietà probabilmente userei questo approccio.

Un modo un po 'migliore è usare un obiettivo condizionale:

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

Qui stiamo di nuovo sfruttando l'immutabilità della proprietà. Se non vuoi farlo, puoi usare l'attributo salvo e un ulteriore livello di riferimento indiretto:

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

È importante notare che gli if e a meno che degli attributi target controllano solo se la proprietà è impostata, non il valore del proprietà.

Altri suggerimenti

Puoi utilizzare la libreria Ant-Contrib per avere accesso a una sintassi < if > < then > < else > , tuttavia richiederà alcuni passaggi di download / installazione.

Vedi questa altra domanda SO: ant-contrib - if / then / else task

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top