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:

<target name="replaceText">
<!--
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):

[include name="**/*.cfm"/]

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:

<replacetoken><![CDATA[<p>
hello
universe
</p>]]></replacetoken>

Will be replaced with this value:

<replacevalue><![CDATA[<p>
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:

<replace file="foo.cfm" token="universe" value="world"/>

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.

Related Blog Entries

Comments
The Ant tutorials are great, keep them coming!
# Posted By Dan Vega | 7/18/07 9:40 AM
Nice. This will come in handy when putting together my Ant script for building Model-Glue applications.
# Posted By Boyan | 7/18/07 10:04 AM
What exactly do you mean Boyan? I'd be curioius to hear what you have in mind, because my next example was going to show renaming .xml files in MG to .xml.cfm for security purposes.
# Posted By todd sharp | 7/18/07 10:17 AM
Yes, great technique. I have used this to set framework modes from development to production during deployment, or changing a DSN value, etc.
# Posted By Brian Kotek | 7/18/07 10:37 AM
This is good stuff. Keep it coming :)

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...).
# Posted By Trond Ulseth | 7/18/07 11:02 AM
Check out Jim's wiki - there are some examples like that up there - I _know_ there is an example interacting with a DB - I _think_ there are others that you mention too...

http://www.thecrumb.com/wiki/Ant
# Posted By todd sharp | 7/18/07 11:07 AM

Calendar

Sun Mon Tue Wed Thu Fri Sat
     12
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex funny stuff misc model-glue off topic personal project learn slidesix sql

Recent Comments

ColdFusion/Sharepoint Integration - Part 1 - Authenticating
todd sharp said: Jenn: Check your email. Todd [More]

ColdFusion/Sharepoint Integration - Part 1 - Authenticating
Jenn said: When does the next issue of FAQU come out? For that matter is it at all possible to get a preview o... [More]

A Major Milestone In My Marriage
Brian Meloche said: I converted my wife last year. That said, I find Firefox 3 a bit crashy. [More]

SlideSix Gets PDF Support
todd sharp said: Do you mean add numbering to the PDF output? If so, yeah, that would be easy. Just to be clear tho... [More]

SlideSix Gets PDF Support
salvatore fusto said: nice work Todd, but can you add slide numbering: 1/N, 2 of N and so long? it would be useful. regard... [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com