Replace the property in the XML file using ANT

I am trying to replace the version number in the build.xml file with an ANT script.

I tried various approaches, searched and re-searched StackOverflow for answers, but couldn't get the exact request.

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 that you can see that the version has yesterday's date. I need to replace it with the current date.

Here is what I 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> 
+7
source share
1 answer

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, copy it to the temp file and return it.

 <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" /> 
+9
source

All Articles