Sunday, 28 February 2016

ANT Build.xml script to replace strings values.

In this post, I will be sharing two scenario's where need to replace string values in all XML files in a folder using ANT tool.




Scenario 1:

Replace various source strings to constant destination string in all XML files in a folder.

a) I placed varying source strings or list of source string into a file called Pattern.txt.
b) My destination string value is XYZ.

Pattern.txt

abccc
abc
ack
acbd

build.xml


  <loadfile property="file" srcfile="pattern.txt"/>
   <for param="line" list="${file}" delimiter="${line.separator}">
   
      <sequential>
                <replace dir="${dir}" value="XYZ">
   <include name="**/*.xml"/>
   <replacetoken>@{line}</replacetoken>
  </replace>
      </sequential>
      </for>


Scenario 2:

Replace various source strings to various destination strings in all XML files in a folder.
- I placed both varying source and destination strings in a file called Pattern2.txt.


Patter2.txt


\general\PartnerFolderLookup.m",\typetrees\gen\PartnerFolderLookup.m"
\gtmax\ShipmentStatus.m",\typetrees\gtmax\ShipmentStatus.m"
\ShippingOrder.m",\typetrees\gtmax\orders\ShippingOrder.m"

In Pattern2.txt file, both source string and destination string is varying and it is separated by a comma.

build.xml



<echo>Replacing Strings using pattern2.txt</echo>
   <loadfile property="file2" srcfile="pattern2.txt"/>
   <for param="line" list="${file2}" delimiter="${line.separator}">
     <sequential>
       <var name="index" value="1"/>
    <for list="@{line}" param="letter">
       <sequential>
           <var name="var${index}" value="@{letter}" />
 <math result="index" operand1="${index}" operation="+" operand2="1" datatype="int" />
       </sequential>
    </for>
   <replace dir="${dir}" value="${var2}" encoding="UTF-8">
       <include name="**/*.xml"/>
       <replacefilter token="${var1}"/>
   </replace>

   </sequential>
       </for>

0 comments:

Post a Comment