2012-08-07 15 views
10

Io uso solo apache-ant e nonant-contribargomenti passa a apache-formica compito exec in base al valore della variabile

Ho un ant bersaglio

<target name="stop" depends="init" > 
... 
</target> 

in cui voglio richiamare exec compito .

Se il valore di una variabile HOST_NAME è all

<exec executable="${executeSSH.shell}" > 
    <arg value="-h ${HOST_NAME}" /> 
    <arg value="-i ${INSTANCE}" /> 
    <arg value="-w 10" /> 
    <arg value="-e ${myOperation.shell} " /> 
    <arg value=" -- " /> 
    <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" /> 
</exec> 

Se il valore di una variabile HOST_NAME è anything else

<exec executable="${executeSSH.shell}"> 
    <arg value="-h ${HOST_NAME}" /> 
    <arg value="-i ${INSTANCE}" /> 
    <arg value="-e ${myOperation.shell} " /> 
    <arg value=" -- " /> 
    <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" /> 
</exec> 

ma vorrei scrivere un solo compito e di non ripetereexec. Ho usato il parametro HOST_NAME ma cosa fare per il secondo parametro -w 10 che è diverso in entrambe le chiamate.

Ho provato un paio di modi effettuando una ricerca su SO utilizzando condition e if else, ma nulla sembra essere applicabile per exec o arg.

risposta

6

Prova a usare macrodef. Il seguente esempio non è testato.

<macrodef name="callSSH"> 
    <element name="extArgs" optional="y"/> 
    <sequential> 
     <exec executable="${executeSSH.shell}" > 
      <arg value="-h ${HOST_NAME}" /> 
      <arg value="-i ${INSTANCE}" /> 
      <extArgs/> 
      <arg value="-e ${myOperation.shell} " /> 
      <arg value=" -- " /> 
      <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" /> 
     </exec> 
    </sequential> 
</macrodef> 
<target name="stop" depends="init" > 
    <if> 
     <equals arg1="${HOST_NAME}" arg2="all"/> 
     <then> 
      <callSSH> 
       <extArgs> 
        <arg value="-w 10" /> 
       </extArgs> 
      </callSSH> 
     </then> 
     <else> 
      <callSSH> 
       <extArgs/> 
      </callSSH> 
     </else> 
    </if> 
</target> 

Oppure, se non si utilizza contributi:

<target name="sshExecWithHost" if="HOST_NAME"> 
    <callSSH> 
     <extArgs> 
      <arg value="-w 10" /> 
     </extArgs> 
    </callSSH> 
</target> 

<target name="sshExecNoHost" unless="HOST_NAME"> 
    <callSSH/> 
</target> 

<target name="sshSwitch" depends="sshExecNoHost,sshExecWithHost"> 
</target> 

<target name="stop" depends="init,sshSwitch" > 
</target> 
10

È possibile utilizzare l'attività condizione:

<condition property="my.optional.arg" value="-w 10" else=""> 
    <equals arg1="${HOST_NAME}" arg2="all" /> 
</condition> 

<exec executable="${executeSSH.shell}" > 
    <arg value="-h ${HOST_NAME}" /> 
    <arg value="-i ${INSTANCE}" /> 
    <arg line="${my.optional.arg}" /> 
    <arg value="-e ${myOperation.shell} " /> 
    <arg value=" -- " /> 
    <arg value="${INSTANCE} ${USERNAME} ${PASSWORD}" /> 
</exec> 
+2

Molto utile, provato mille varianti e questo è finalmente cosa ha fatto il trucco. –

+0

Questo è grande e compatto. L'ho usato per generare condizionalmente report di copertura del codice basati su un parametro di build. –

Problemi correlati