Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull out a substring in Ant

Tags:

ant

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

like image 267
Lark Avatar asked Jun 03 '09 15:06

Lark


4 Answers

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>
like image 86
seduardo Avatar answered Nov 03 '22 15:11

seduardo


You could try using PropertyRegex from Ant-Contrib.

   <propertyregex property="destinationProperty"
              input="${sourceProperty}"
              regexp="regexToMatchSubstring"
              select="\1"
              casesensitive="false" />
like image 23
Instantsoup Avatar answered Nov 03 '22 17:11

Instantsoup


Since I prefer to use vanilla Ant, I use a temporary file. Works everywhere and you can leverage replaceregex to get rid of the part of the string you don't want. Example for munging Git messages:

    <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>
like image 10
Bill Birch Avatar answered Nov 03 '22 16:11

Bill Birch


I guess an easy vanilla way to do this is:

<loadresource property="destinationProperty">
    <concat>${sourceProperty}</concat>
    <filterchain>
        <replaceregex pattern="regexToMatchSubstring" replace="\1" />
    </filterchain>
</loadresource>
like image 6
Markus Rohlof Avatar answered Nov 03 '22 16:11

Markus Rohlof