Ant 101 - Replacing Strings Within A File
I got some positive feedback on my intro to Ant post yesterday so I've decided to move forward with the topic and post a real life example of using Ant. Attached is the full source of this demo.
In this example I'm going to show how to do something with Ant that I think is pretty common - replacing all occurrences of a substring with another substring within a given file. Now I know what you're saying. "Why would I want to write a script to do that when I could easily do it [another way]?" And that's a valid argument. If you have a way to do it quicker/easier/better then cool, go for it. The purpose of this demo is to show that you can do a quick find and replace on multi-line strings within Eclipse - something that I've been unable to figure out how to do until I started messing with Ant (which does not mean the functionality is not there - it may be - I just haven't figured out how to do it yet).
Bring on the code.
OK - replacing a given substring with another substring. Ant gives us the <replace> tag for just such occassions. Check out the following code:
<!--
replace all occurences of the string
'universe' with the string 'world' in the current
directory
-->
<replace dir=".">
<!-- the include just filters on .cfm templates -->
<!-- i had to use [] instead in this demo because blogcfc tries to parse the include -->
[include name="foo.cfm"/]
<!-- string to replace -->
<replacetoken><![CDATA[<p>
hello
universe
</p>]]></replacetoken>
<replacevalue><![CDATA[<p>
hello
world
</p>]]></replacevalue>
</replace>
</target>
There's a lot going here so let me break it down a bit. The outer <target> tag wraps this individual bit of activity and is part of a larger <project> that is not being shown here (check out the download attached). Inside of our target we have a <replace> task that we have passed a single 'dir' attribute to. The value "." tells Ant that the activity is happening in the current directory (same directory as the build.xml file). Next the <include> tag points Ant to a single file (foo.cfm). I could have told Ant to search all files by changing the explicit file name to this (forgive the [] - blogCFC tries to parse all includes):
Pretty simple so far, no? Next we simply tell Ant what token to replace with what value. The code in this part looks a bit nasty - but the formatting is necessary since we are looking to replace a multi-line token with a multi-line value. Also note the strings are wrapped in a <![CDATA[ ]]> to prevent Ant from trying to parse any tags inside of the strings. So this token:
hello
universe
</p>]]></replacetoken>
Will be replaced with this value:
hello
world
</p>]]></replacevalue>
Pretty cool. If we were just doing a simple text value (non multi-line) we could have simplified this entire process into a single line of code like so:
That is all there is to it. In my next example I'll show how to quickly and easily rename a file (or entire directory of files) and why you'd want to do that. The code for the next entry is available in the attached zip if you'd like to check that out.



Now that I see how ANT can be used a lot of questions starts to pop up, IE how far is it possible to go (interacting with SVN, FTP, DB server etc...).
http://www.thecrumb.com/wiki/Ant