tags:

views:

3167

answers:

4

Is there a way to pull a substring from an Ant property and place that substring into it's own property?

+1  A: 

I would go with the brute force and write a custom Ant task:

public class SubstringTask extends Task {

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

What's left it to implement the String process(String) and add a couple of setters (e.g. for the oldproperty and newproperty values)

Vladimir
+3  A: 

You could try using PropertyRegex from Ant-conrtib.

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

i would use script task for that purpose, i prefer ruby, example cut off the first 3 chars =

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

output =

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

using the ant api with method project.setProperty() on an existing property will overwrite it, that way you can work around standard ant behaviour, means properties once set are immutable

Regards, Gilbert

Rebse
A: 

I use scriptdef to create a javascript tag to substring, for 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>
seduardo