Ant 101 - Intro & Why You Should Check It Out

Last week I decided to take a look at Ant. I have heard good things about it in the past, but never really took notice of what it could do until recently. I hesitated on posting this because after I did some manual digging and learning I discovered that Andy Jarrett has quite a number of very helpful posts about Ant but I decided to go ahead and post it as an intro to folks who are new to it.


So what is Ant?

Well the answer according to the FAQs on ant.apache.org tell us:

Ant is a Java-based build tool. In theory, it is kind of like Make, without Make's wrinkles and with the full portability of pure Java code.

Nice. But I don't have a clue what the hell Make is so that description is no good to me. Reading a little further we see the following:

According to Ant's original author, James Duncan Davidson, the name is an acronym for "Another Neat Tool".

Later explanations go along the lines of "ants do an extremely good job at building things", or "ants are very small and can carry a weight dozens of times their own" - describing what Ant is intended to be.

So that explains it a little better. Basically Ant helps you automate monotonous repetitive tasks like moving/renaming files and directories, zipping and unzipping, replacing text strings within a file or group of files, and much more. Why would a ColdFusion developer care about Ant? Picture a script that pushes your development sites to production with one click. Now you begin to see the power of Ant.

How difficult is it to learn Ant?

At it's core Ant is nothing more then a tag based syntax. Sweet. I like tags. They're easy to use and I'm used to them since I use CF and Flex.

So how 'bout some code?

You can install Ant outside of Eclipse, but since it's already built into Eclipse I think it's a little nicer to take advantage of that fact and use the built in version. An Ant project is created in Eclipse by simply creating a file called build.xml and saving it in a project. Immediately you'll notice that Eclipse recognizes the Ant script and identifies it in your Project Navigator with an icon with, well, an Ant.

The root of every Ant project is the <project> tag. So far, so good. The project tag will contain one or more <target> tags. These represent individual milestones in the project. The name attribute is required to ensure unique targets. Within each target are the actual tasks to be performed such as <move>, <copy>, etc (for full documentation of the available tasks see resources below). So here's a simple example taken from the Getting Started With Ant guide that I also link to below in resources.

<project default="hello">
   <target name="hello">
      <echo message="Hello, World"/>
   </target>
</project>

Paste that into Eclipse and save as build.xml. You can run the task several ways. The first option is typing Alt+Shift+X, Q. Eclipse will prompt you to save any unsaved files and should then output the following in the console:

Buildfile: C:\ColdFusion8\wwwroot\test8\build.xml
hello:
[echo] Hello, World
BUILD SUCCESSFUL
Total time: 203 milliseconds

What about having a project with multiple targets? Well that may look like this example (again borrowed from the Getting Started guide):

<project default="hello">
   
   <target name="hello">
      <echo message="Hello, World"/>
   </target>

   <target name="goodbye">
      <echo message="Goodbye, Cruel World"/>
   </target>
   
   <target name="all" depends="hello,goodbye" />
   
</project>

I've jumped a bit ahead because I think this is basic stuff at this point and introduced a third target named 'all' with an attribute of depends. This tag tells Ant about tasks that have dependencies on one another. You can imagine how this could be valuable in more complex projects. So go ahead and run this task in Eclipse. You'll notice that we get the same 'Hello World' echo but not the 'Goodbye' echo. In this case Ant ran our default target that was specified in the <project> tag. To get all the targets to run we can select Run - External Tools - External Tools and we are presented with the menu illustrated below. If we choose the target tab we will see all of our targets listed.

Select All - Apply - Run and you'll see the following in the console.

Buildfile: C:\ColdFusion8\wwwroot\test8\build.xml
hello:
[echo] Hello, World
goodbye:
[echo] Goodbye, Cruel World
all:
BUILD SUCCESSFUL
Total time: 203 milliseconds

So thats about it for a basic intro to Ant. I could go further with a more real life use case, but I'm not sure. Would there be interest in that? Andy's blog does have some very good examples and I'd hate to rehash what he's already touched on.

Resources

Andy Jarrett's Ant Category
Getting Started With Ant
Ant Manual



Related Blog Entries

Comments
FYI - I have a lot of Ant links on my wiki:

http://www.thecrumb.com/wiki/Ant

And I just added yours :)
# Posted By Jim Priest | 7/17/07 10:00 AM
Nice work, Todd! I'm sure a lot of newbies will find this helpful. There can never be enough simple walk-through write-ups.

What makes this helpful is the bringing together combined knowledge, and that in itself is always a good thing.

Keep going!
# Posted By Jurgen Beck | 7/17/07 10:00 AM
I say "keep'em coming". After reading this post I finally understand somewhat what Ant is and what it can be used for. I headed over to Andys blog and there's lot of great stuff there. But I like the way you explain stuff - and a good thing can't be repeated to often :)
# Posted By Trond Ulseth | 7/17/07 10:00 AM
Bring 'em on! As recent converts to CFEclipse, we see the tool there. We'd like to know how we could fit it into our processes...and the more examples the better.
# Posted By Eric Hoffman | 7/17/07 10:47 AM
I have been wanting to get started using Ant also, so I look forward to additional posts on the topic.
# Posted By Jeff Fleitz | 7/17/07 11:44 AM
@Jim - Awesome, did not know about that resource either.

@Everyone else - cool. I'll post a real life example within the next few days. Thanks.
# Posted By todd sharp | 7/17/07 1:40 PM
Awesome! Thanks for posting this. I always wanted to learn how to work with ANT but I have to admit that I was a bit intimidated by the fact that is Java based (and all those tags).

Btw, if you really don't know what "make" is: it's a build tool for C++. You create a "make" file (called excatly "makefile") that defines what happens when you compile a program in C++. Usually, in C++ you need prerequisites before you can build the final executable (such as including headers and other libraries), so make automated that by looking for the makefile in the same directory and executing the specified tasks before the actual build.
# Posted By Boyan | 7/18/07 9:55 AM
Thanks Todd - real life examples are always good...
# Posted By Dan Lancelot | 7/25/07 5:52 PM
Can't be better. Simple & concise. Straight to the point. This is what I was looking for.
# Posted By Stephane | 8/7/07 8:31 AM
I am just getting back int the swing of Ant so thanks for this article. Just something I noticed that may help. You do not have to change the default in Eclipse. You can assign a default from your project tag like so.

<?xml version="1.0" encoding="utf-8"?>
<project name="myproject" default="all">
   <target name="all" depends="hello,goodbye"/>
   
   <target name="hello">
      <echo message="Hello, world!"/>
   </target>
   
   <target name="goodbye">
      <echo message="Goodbye!"/>
   </target>
   
</project>
# Posted By Dan Vega | 1/18/08 2:24 PM

Calendar

Sun Mon Tue Wed Thu Fri Sat
   1234
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 javascript misc model-glue off topic personal project learn slidesix sql

Recent Comments

Chinese Birth Calendar Accuracy Test
mama to be said: ok so i will be 2 months shy of 18 when i have my baby. this calendar does not technically work for ... [More]

Fixing 'User Profile Service Failed The Logon' on Vista
Mike said: That fix worked although all i did was remove .bak and reset state to 0. User was able to log in to... [More]

Chinese Birth Calendar Accuracy Test
Melissa said: Wrong for my daughter, which it predicted to be a boy... we'll see for #2. Predicts a girl (maybe, f... [More]

Adding Auto Generated Code Downloads to BlogCFC
fweerw said: http://www.ibiblio.org/st... http://www.cambodia.ait.a...... [More]

Check Out The New SlideSix
Todd Sharp said: Thanks for the feedback Ben & Rachel! I'll keep it all in mind as I tweak things over the next ... [More]

RSS


adobe community experts

coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com