tags:

views:

343

answers:

2

I need to replace all occurrence of some regular expression in xml file with proper values loaded from property file. As example

  • in xml file I have < port=${jnpPort}/>
  • in property file I have port=3333

I want to have xml file with entries like < port=3333/>

Now using

<replaceregexp match="\$\{(.*)\}" replace="${\1}" flags="g" byline="true">
        <fileset dir="." includes="file.xml"/>
</replaceregexp>

I get pretty much the same <port=${jnpPort} />. I would like value of ${jnpPort} were read from property file.

A: 

You just use copy with a filterset

<filterset id="version.properties.filterset" begintoken="$" endtoken="$"> <filter token="jnpPort" value="${port}" /> </filterset>

<copy file="file.xml.template" tofile="file.xml" overwrite="true" > <filterset refid="version.properties.filterset" /> </copy>

Okay, not quite copy in place, but pretty good.

Decado
Ok thanks. It will work and I have tried this, but I am looking for something more generic. I do not want to define values which should be replaced but it should be based on regular expression. For example in one file there will be different number of < port=${something} /> combination. And I would like that ${something} will determine which value will be set from property file.
A: 

Try:

<replaceregexp match="@< port=\${jnpPort}/>@" replace="@< port=$(port)/>@" flags="g" byline="true">
        <fileset dir="." includes="file.xml"/>
</replaceregexp>
codaddict