Using Ant to change text in files revisited

A few years ago I posted a blog entry that showed how to replace using template files, for a catch up on that entry head on over to Eclipse & Ant tidbit for further reading.

However I ended up modifying that because it was a little bit annoying when a developer on my team, would modify the wrong files. So I looked at another option.

So rather than use the replace tokens in my other post, I looked at the ant tag replaceregexp to do the job for me. You are probably thinking what is the point of that, and at times there won't be a great need for it. But it can make the smallest of changes automated for you if you wish to go from development to production, as well as a heap of other uses.

In my case while in development I currently use the Application.cfc and the Application.name to provide the version number of the Application that I am currently developing, the problem is manually changing this in a number of files. So we have Ant to the rescue to do this for us, the following target is not very pretty as I am not a regexp guru and sometimes struggle to comprehend the syntax. So if anyone ever cleans it up with back references then please do drop me a line.

<target name="Update build number">
<tstamp>
   <format property="dateYear" pattern="yyyy" locale="en,AU"/>
   <format property="dateMonth" pattern="MM" locale="en,AU"/>
   <format property="dateDay" pattern="dd" locale="en,AU"/>
</tstamp>
   <propertyfile file="${basedir}\build\build.properties" comment="Our build properties">
    <entry key="dateYear" value="${dateYear}"/>
    <entry key="dateMonth" value="${dateMonth}"/>
    <entry key="dateDay" value="${dateDay}" />
   </propertyfile>
   <buildnumber file="${basedir}\build\build.properties" />
   <echo>Building ${package.name} V0.1.${dateYear}.${dateMonth}.${dateDay}.${build.number}</echo>
   <replaceregexp match="&lt;cfset AppName = &quot;Springfield V(.*?)&quot; /&gt;" replace="&lt;cfset AppName = &quot;Springfield V0.1.${dateYear}.${dateMonth}.${dateDay}.${build.number}&quot; /&gt;" flags="gim" byline="false">
      <fileset dir="${basedir}\www" includes="**/*.cfc"/>
   </replaceregexp>
</target>

Now you can see that from the above code that I am using some extra tags to get the Year, Month and Day and then store it into the properties file called build.properties. I then also use the buildnumber and have that stored in there as well.

Now if you browse my blogs footer you will notice that there is the same string down the bottom there, and the above script ensures that when I build the application the Application scope is also changed to reflect the current build number. I do this for a number of reasons and the main one, is that the Application scope automatically is cleared of all variables in the scope. Making it easier to debug if I need to, I hope you enjoy this small example of how to use Ant within Eclipse during development of ColdFusion Applications.