I'm trying to replace a version number in a build.xml file using an ANT script.
I've tried various approaches, searched and re-searched StackOverflow for answers but could not get the exact query.
so here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<project name="feature" default="main" basedir=".">
<target name="init">
<property name="Version" value="1.0.0.20120327"/>
</target>
<target name="main" depends="init">
<description>Main target</description>
</target>
</project>
Now as u can see the Version has yesterday's date. I need to replace it with the current date.
Here is what I've tried:
<target name="replace">
<tstamp >
<format property="touch.time" pattern="yyyyMMdd"/>
</tstamp>
<property name="Feature.dir" location="../feature" />
<!--Didnt Work-->
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterset>
<filter token="Version" value="1.0.0.${touch.time}"/>
</filterset>
</copy>
<!--Didnt work
<replacetoken><![CDATA[<property name="Version" value=""/>]]>
</replacetoken>
<replacevalue><![CDATA[<property name="Version"value="1.0.0.${touchtime}" />]]>
</replacevalue>
-->
<!-- Didnt work
<copy file="${Feature.dir}/build.xml" tofile="${Feature.dir}/build1.xml" >
<filterchain>
<tokenfilter>
<replaceregex pattern="^[ \t]*Version[ \t]*=.*$"
replace="Version=1.0.0.${touch.time}"/>
</tokenfilter>
</filterchain>
</copy>
-->
</target>
I would use replaceregex
inside a filterchain
.
For example:
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterchain>
<tokenfilter>
<replaceregex pattern="1.0.0.[0-9.]*" replace="1.0.0.${touch.time}"/>
</tokenfilter>
</filterchain>
</copy>
If you want to replace the file, feel free to copy to a temp file and move it back.
<tempfile property="build.temp.file.name"/>
<copy file="${Feature.dir}\build.xml" tofile="${build.temp.file.name}" ... />
<move file="${build.temp.file.name}" tofile="${Feature.dir}\build.xml" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With