Ant 101 - Replacing Strings Within A File

Posted By : todd sharp Posted At : July 18, 2007 5:06 AM Posted In: Ant

7

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 (7)

Dan Vega's Gravatar The Ant tutorials are great, keep them coming!

Boyan's Gravatar Nice. This will come in handy when putting together my Ant script for building Model-Glue applications.

todd sharp's Gravatar 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.

Brian Kotek's Gravatar Yes, great technique. I have used this to set framework modes from development to production during deployment, or changing a DSN value, etc.

Trond Ulseth's Gravatar 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...).

todd sharp's Gravatar 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

coach bags outlet's Gravatar I really like your article, I support your point of view