Agile Zone is brought to you in partnership with:

Meera has posted 70 posts at DZone. You can read more from them at their website. View Full User Profile

Ant or Gant?

09.23.2008
| 41193 views |
  • submit to reddit

Yes, this is exactly what I am frequently asked by my clients and many developers. It isn't easy to answer this question. There are several  projects using Ant. Should you run away from Ant just because there is a new cool tool out there called Gant? Should you switch to Gant just because you dislike XML? Not at all. Let's take a closer look and see what might make you switch to Gant.

When to choose Gant?

1. Complicated Build Files. If your ant build files are becoming too complicated, and hard to manage, it's time to see if using Gant can help. Let me explain what I mean by complicated build files. If you have too much of conditional logic within your build files, say something similar on the lines shown below in Listing 1:

Code Listing 1:

    <if>
<isset property="sqlserver"/>
<then>
<do something here/>
</then>
</if>
<if>
<isset property="oracle"/>
<then>
<do something else here/>
</then>
</if>
<if>
<isset property="derby"/>
<then>
<do something for derby here/>
</then>
</if>
<if>
<isset property="db2"/>
<then>
<do something for db2 here/>
</then>
</if>

 Or even something like this where you might be supporting deployment to different application servers based on some property in your build.properties as shown in listing 2.

Code Listing 2:

    <if>
<isset property="server.jboss4"/>
<then>
<deploy to JBoss 4/>
</then>
<elseif>
<isset property="server.weblogic10"/>
<then>
<deploy to web logic/>
</then>
</elseif>
<elseif>
<isset property="server.glassfish"/>
<then>
<deploy to GlassFish/>
</then>
</elseif>
<elseif>
<isset property="server.someother version"/>
<then>
<deploy to this some other version/>
</then>
</elseif>
</if>
Things get out of hand  when you have conditional logic as shown above in your build scripts. The listings I have are just the skeleton, imagine what happens when we start adding the actual deployment logic for all these application servers. It doesn't matter how you refactor this, it is still going to be very complicated. Trust me, I have written build scripts which were several thousand lines, and refactoring them was not a trivial task.

2. Custom Ant Tasks. I myself am guilty of writing many of these. There are many situations which arise in projects where we create custom ant tasks. It is simple once you know how to write one, and than for every complicated task you need to perform, you involuntarily will start writing custom ant tasks.

Anyone writing a custom ant task will:

a. Create a new class that extends Ant’s org.apache.tools.ant.Task class.

b. For each attribute, write a setter method.

c. Write an execute()method that does what you want this task to do.

There isn't anything wrong in doing the above, but imagine each time you want to make a small change, you will have to make changes within your Java source code, compile, test, and re-package.

3. Scripting. You can extend Ant further by not writing custom ant tasks, but by using small snippets of code written in an interpreted language like JRuby, BeanShell, or Groovy. These code snippets can be placed within your build files or in separate text files. If you are using Groovy's Ant task, your build file might look something like this:

Code Listing 3:

 <groovy classpathref="build.classpath">
import some.package
import another.package

def fullpath = "${.basedir}/${defaulttargetdir}"

def somefile = new SomeFile(projectName:"${pname}",
buildLabel:"${label}", buildTime:"${new Date()}")

def xml = "${fullpath}/dashboard.xml"

new File(path).write(somefile.generateReport())

ant.xslt(in:path,
out:"${properties.defaulttargetdir}/some.html",
style:"${properties.defaulttargetdir}/lib/report-style.xsl")
</groovy>

Imagine having several lines of XML in your build files which have many of these small snippets of scripts. I myself don't like mixing and matching build files with code snippets. If you have a team where everyone is in the same page, everything works fine. What if a team member has no clue about any of the Scripting languages? He/She will have no clue how to make minor changes when things go badly. If you have all the above or even one of the above three cases, you seriously need to consider using Gant. To quote Aristotle:

For the things we have to learn before we can do them, we learn by doing them."


So, lets see how easy it is to learn Gant and see how things can improve.

This part covers the very basics of Gant. The next part, will dive deeper into Gant by using it with a sample project to build an application, and we will also see how to use it with our CI Server.


Published at DZone with permission of its author, Meera Subbarao.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mike P(Okidoky) replied on Mon, 2008/09/29 - 10:54pm in response to: phil swenson

I want to be able to write (or use someone else's) template scripts, and use it to create buildable projects from it. I want to be able to open a text editor, type in the name of the project, the names of the java files, jar library files, and the name of the script that it needs to process it.

That way, I can keep upgrading the scripts without having to touch each project.

Serge Bureau replied on Tue, 2008/09/30 - 7:13am in response to: Mike P(Okidoky)

[quote=okidoky]

I want to be able to write (or use someone else's) template scripts, and use it to create buildable projects from it. I want to be able to open a text editor, type in the name of the project, the names of the java files, jar library files, and the name of the script that it needs to process it.

That way, I can keep upgrading the scripts without having to touch each project.

[/quote]

 

Why should I need any XML/XSL for that ?

phil swenson replied on Tue, 2008/09/30 - 9:03am in response to: Serge Bureau

"Why should I need any XML/XSL for that ?"

 You don't.  That's what is so great about using a real language, you can make the build do whatever you want.

Kenneth Wheeler replied on Fri, 2009/11/20 - 6:12am

I agree with what you said about Ant or Gant?. Get Tall Ant or Gant?. How to Grow Taller Naturally Ant or Gant?. Growing Taller Secrets

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.