0

Please help me here with ant script to perform below mentiones task as i am stuck with it and not getting through.

I have properties files as below:

AccessSession/OperatorCode=Production

AccessSession/Password=%587931#

And so on....

And XML content is as below:( shortened xml content)

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>TM Production</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>%T3lkom9525#</value>
    </NameValuePair>

And so on....

I want script to update value tag in XML file with actual property value given in properties file

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>Production</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>%587931#</value>
    </NameValuePair>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185

1 Answers1

0

I suggest using a filterset in the ANT copy task to substitute values into a template file.

Example

├── build.properties
├── build.xml
├── src
│   └── template.xml
└── target
    └── output.xml

build.properties

AccessSession/OperatorCode=Production
AccessSession/Password=%587931#

build.xml

<project name="demo" default="build">

  <property file="build.properties"/>

  <target name="build">
    <copy file="src/template.xml" tofile="target/output.xml">
      <filterset>
        <filter token="OPERATOR_CODE" value="${AccessSession/OperatorCode}"/>
        <filter token="PASSWORD" value="${AccessSession/Password}"/>
      </filterset>
    </copy>
  </target>

</project>

src/template.xml

Template that contains the replaceable tokens

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>@OPERATOR_CODE@</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>@PASSWORD@</value>
    </NameValuePair>

target/output.xml

Output file generated by the ANT build.

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>Production</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>%587931#</value>
    </NameValuePair>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Hi Mark..Well XML file is very big and will have more than 20-30 name/value pair.This count is actually dynamic for all applications.So there will be manual intervention to create template.xml file and manually edit all value pairs with replaceable token.Any way we can place repleacebale tokens in automated way because as i said number of value pairs is dynamic based on application. – Shrijeet Sinha Apr 15 '17 at 10:21
  • @ShrijeetSinha, this answers the original question very well. Now you are asking the new questions which is entirely different from original one. You consider accepting answer and add new question to handling of dynamic properties. – Rao Apr 15 '17 at 23:51
  • @ShrijeetSinha If you need more powerful capabilities (for example the ability to resolve adhoc properties within the template) then why not consider a proper template engine like FreeMarker http://freemarker.org/docs/pgui_misc_ant.html (And there are lots of options available). Another option would be to embed a scripting language like groovy within your build if you need to write a specific XML file. Which option is best in your circumstance is difficult to determine at this point. – Mark O'Connor Apr 17 '17 at 12:21