2009-06-03 15 views
31

C'è un modo per estrarre una sottostringa da una proprietà Ant e posizionare quella sottostringa nella sua proprietà?Come estrarre una sottostringa in Ant

+0

Puoi essere più specifico su cosa stai cercando di fare? Perché non avrebbe più senso definire una proprietà per contenere il valore che stai dipendendo e utilizzarla in più posti? Se le tue proprietà Ant cambiano abbastanza spesso da dover reagire in modo programmatico, potresti fare qualcos'altro che non va. – PanCrit

risposta

33

Io uso scriptdef per creare un tag javascript per stringa, per exemple:

<project> 
    <scriptdef name="substring" language="javascript"> 
    <attribute name="text" /> 
    <attribute name="start" /> 
    <attribute name="end" /> 
    <attribute name="property" /> 
    <![CDATA[ 
     var text = attributes.get("text"); 
     var start = attributes.get("start"); 
     var end = attributes.get("end") || text.length(); 
     project.setProperty(attributes.get("property"), text.substring(start, end)); 
    ]]> 
    </scriptdef> 
    ........ 
    <target ...> 
    <substring text="asdfasdfasdf" start="2" end="10" property="subtext" /> 
    <echo message="subtext = ${subtext}" /> 
    </target> 
</project> 
2

vorrei andare con la forza bruta e scrivere un compito Ant personalizzato:

public class SubstringTask extends Task { 

    public void execute() throws BuildException { 
     String input = getProject().getProperty("oldproperty"); 
     String output = process(input); 
     getProject().setProperty("newproperty", output); 
    } 
} 

Ciò che rimane è di attuare la String process(String) e aggiungere un paio di setter (ad esempio, per i valori oldproperty e newproperty)

21

Si potrebbe provare a utilizzare PropertyRegex from Ant-conrtib.

<propertyregex property="destinationProperty" 
       input="${sourceProperty}" 
       regexp="regexToMatchSubstring" 
       select="\1" 
       casesensitive="false" /> 
+0

Nota aggiuntiva: nel caso in cui ciò avvenga dinamicamente in un ciclo, usare 'override =" true "' per sovrascrivere qualsiasi valore precedente. – robinst

0

userei compito scritto a tal fine, i preferiscono rubino, esempio tagliate i primi 3 caratteri =

<project> 
    <property name="mystring" value="foobarfoobaz"/> 
    <target name="main"> 
    <script language="ruby"> 
    $project.setProperty 'mystring', $mystring[3..-1] 
    </script> 
    <echo>$${mystring} == ${mystring}</echo> 
    </target>  
    </project> 

uscita =

main: 
    [echo] ${mystring} == barfoobaz 

utilizzando l'api formica con metodo project.setProperty() su una proprietà esistente lo sovrascriverà, in questo modo è possibile aggirare il comportamento standard ant , significa propert Una volta impostato i sono immutabili

+0

Ciao, ho ottenuto questo errore: Impossibile creare script engine javax per ruby ​​ –

+0

Jarod, è necessario bsf.jar (http://jakarta.apache.org/bsf) e jruby.jar (http: //www.jruby .org), vedi anche http://ant.apache.org/manual/install.html#librarydependencies, nota che il link a jruby è sbagliato su quella pagina. – Rebse

+0

oh grazie Rubse –

8

Dal momento preferisco usare la vaniglia Ant, io uso un file temporaneo. Funziona ovunque e puoi sfruttare replaceregex per sbarazzarti della parte della stringa che hai non desiderata. Esempio di messaggi Git munging:

<exec executable="git" output="${git.describe.file}" errorproperty="git.error" failonerror="true"> 
     <arg value="describe"/> 
     <arg value="--tags" /> 
     <arg value="--abbrev=0" /> 
    </exec> 
    <loadfile srcfile="${git.describe.file}" property="git.workspace.specification.version"> 
     <filterchain> 
      <headfilter lines="1" skip="0"/> 
      <tokenfilter> 
       <replaceregex pattern="\.[0-9]+$" replace="" flags="gi"/> 
      </tokenfilter> 
      <striplinebreaks/> 
     </filterchain> 
    </loadfile> 
+4

Se hai a che fare con una proprietà per iniziare, puoi evitare il file facendo qualcosa come $ {property.here} Scott

+0

Grazie! Ho usato '' invece di exec e ha funzionato perfettamente –

Problemi correlati