Home > Articles > Programming > Java

Ant: Building a Better Build File

Marcus Zarra
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowMarcus Zarra

Marcus Zarra

Learn more…

Marcus Zarra and Matt Long on Core Animation
Jun 5, 2009
Introduction to Migration in Apple's Core Data
Mar 7, 2008
A Look at Apple's Core Animation
Feb 1, 2008
The State of Java Development on OS X
Jan 4, 2008
Packaging Your Application for OS X
Nov 21, 2007
Tiger vs. Leopard: Which Should You Develop For?
Oct 26, 2007
Building on Apple Sync Services
Sep 8, 2006
Getting in Sync with OS X
Sep 1, 2006
Syncing Your Data the Cocoa Way
Aug 11, 2006
Controlling Both Ends of the Communications Channel: From Cocoa to Servlets
Jun 2, 2006
Cocoa: Working with XML
May 19, 2006
Java EJB 3.0: A Hibernate Clone?
Apr 28, 2006
Document Level Parameters Using Core Data: A Guide for Cocoa Developers
Apr 7, 2006
Tips for J2EE Development
Feb 24, 2006
The Cross-Platform Java GUI: Designing Code for More Than One Operating System
Feb 10, 2006
Avoiding Java Exception Abuse
Dec 22, 2005
Java Perspective: Advanced Core Data
Dec 16, 2005
Java Perspective: Cocoa-Java Bridge
Dec 1, 2005
Java Perspective: Core Data and the Mac OS X Developer
Nov 23, 2005
Java Perspective: Key-Value Protocols, Behind the Magic of Mac OS X Development
Nov 11, 2005
Java Perspective: Cocoa Subclasses and Delegates
Nov 4, 2005
The Java Perspective: Cocoa's Interface Builder
Oct 21, 2005
Maven: Handling Multiprojects
Sep 23, 2005
Java, Maven, and Your Web Development Project
Aug 26, 2005
Ant User, Let Me Introduce Maven
Jul 29, 2005
Using JAAS Authentication with JBoss
Jun 17, 2005
Building a Custom JBoss Login Module
Jun 10, 2005
Testing Java Enterprise Applications with Cactus
May 20, 2005
Ant: Building a Better Build File
May 6, 2005
XDoclet: Entity Bean Relationships
Apr 8, 2005
The Java Developers' Introduction to XDoclet
Jan 7, 2005
An Introduction to Cactus
Dec 30, 2004
Using Multiple Databases with JBoss
Nov 24, 2004
JBoss Application Configuration Breakdown
Nov 5, 2004
Sortable Swing JTable
Oct 1, 2004
Building a Professional Swing JTable
Sep 10, 2004
Creating a Custom Java Swing Tablemodel
Aug 27, 2004
Adding Workflow Control to Your Java Applications
Mar 26, 2004

Sorry, this author hasn't posted any blogs.

Want to keep your build file lean, mean, and totally maintainable? Marcus Zarra tells you why Jakarta's Ant is a relatively easy-to-use way to build your projects (and how to avoid its common pitfalls).

From the developer who uses VIM or Emacs to the developer who uses a more complex IDE such as NetBeans, Eclipse, or IntelliJ's Idea, Jakarta's Ant has made their life much easier and more consistent. However, there does seem to be a growing trend to make build files that are overly complex and difficult to maintain. I doubt that anyone starts out writing a build file with the goal of making it un-maintainable, but as a project grows, the build tends to become more and more complex—often with multiple targets that do the same thing in a slightly different way. Listed in this article are a few ways to keep your build file lean, mean, and totally maintainable.

K.I.S.S.

Although this may seem like old advice for a seasoned programmer, it bears repeating: Avoid having Ant targets that do more than one task unless it is logical for them to be combined. For instance, imagine a target that does the following:

  • Removes old class files
  • Compiles all source code
  • Creates a Java archive of all class files
  • Creates a Web archive of all files
  • Copies the Web archive over to the development server

Although it is definitely a useful target, its scope is quite limited. Unless you want to always completely rebuild and redeploy the application you are working on, this target will not see very much use. Consider using several smaller targets with the following names instead:

  • clean
  • compile
  • jar
  • war
  • deploy
  • all

With these targets, you can then assign dependencies as follows:

  • jar depends on compile
  • war depends on jar
  • deploy depends on war
  • all depends on clean and deploy

In this situation, there is a single target that does everything that the previous large target did by calling ant all. In fact, you can set this up as the default target; you can merely call ant to rebuild the entire project. But, more importantly, if you just want to recompile the class files you just modified to ensure that they compile, you can do that without having to deploy everything.

By keeping the targets small and simple, the build file becomes more flexible, useful, and easier to maintain.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network