Jan 03

Quick Tip: Using “Ant Builder” to customize Eclipse builds using ant scripts

Tag: ant, development, eclipse, javapmularien @ 11:09 pm

Recently, while working on a customer’s older EJB 2-based system, I was going a bit nutty having to run a 1-2 minute ant build to execute various code generation tasks (anyone have fond memories of ejbgen like I do? ;) )

I was able to get Eclipse set up to be happy with the relatively complex build structure, and happily discovered the “Ant Builder” capability of Eclipse, which allows ant build tasks to be inserted into the Eclipse build process. Tying this external build process into the IDE helped improve my efficiency while working on this complex project. I’ll go over here briefly how you can use this feature through an illustrative example.

The Eclipse documentation (linked above) describes how to use this feature in much more detail than I can, but here’s a couple caveats I ran into which may help you.

  • Targets you set for “auto build” should be really fast. Remember that Eclipse auto-builds (depending on your project setup) can fire every time you save. It can be very painful to wait even 5-10 seconds for an ant task to kick off after you save.
  • Do not make the common mistake (even though the Eclipse tutorial illustrates this) of setting your default ant target to fire off, unless you really mean it. Remember that Eclipse will be doing the Java compiling for you (assuming you added the ‘Java Builder’ to your project Builders list), so you can omit this ant task.
  • If you’re linking multiple ant files (my project involved 10 ant scripts), I would suggest that if you set a target for the ‘clean’ action, you do this in only one ant file, and allow ant to cascade down through other build files as necessary.
  • Remember to use the ‘refresh’ tab when configuring an ant builder that involves code generation to refresh project resources. Refresh the smallest possible part of your project, and ideally only refresh right before the Java Builder kicks off (on large projects, refresh can be very slow). Unfortunately, one problem that I have found with refresh is that I do not believe the refresh will occur if an error occurs as part of the ant build. This can require you to do a manual refresh in Eclipse if you’ve done a partial build and want to pick up any ant-generated code.

Concrete Example

Just to bring this down to an actual example, here’s what my project looked like:

On Auto Build

I had no ant tasks configured for auto-build. As I mentioned earlier, the lag between save and completion of save while ant tasks ran was driving me crazy - if you have fast ant tasks, this may be an option for you.

On Manual Build

Remember that tasks labeled ‘on manual build’ will only kick off when you manually invoke a build (Ctrl+B). As a note, where I refer to ‘ant’ below, I mean an “Ant Builder”.

  1. 8 ant scripts, each running EJBGen (really, this could be folded down into a single script calling EJBGen 8 times - would probably be slightly faster).
  2. One last script running EJBGen. This single script was configured to refresh the generated source folders using a working set (since the project contains 2000+ files, refreshing the whole project wasn’t practical).
  3. Java Builder
  4. Faceted Project Validation Builder (to validate JSPs and such)
  5. Validation
  6. Ant target for WAR/EAR packaging and deployment to server

On Clean

I picked a single top-level ant script that would remove all generated code and output distributables, with a suitable refresh set. Remember that you don’t have to define ant targets for any build step if you don’t want to. This can let you set up, for example, an ant builder whose sole purpose is to handle the ‘clean’ task.

Caveats

I ran into an issue where, after some time using this technique, Eclipse would simply stop executing all the configured build steps, even after a restart. I posted to the Eclipse newsgroups, and received some helpful tips on enabling debugging switches. Unfortunately, I had, in the process of installing some unrelated Eclipse plugins, done an ‘eclipse -clean’, which seemed to resolve the ‘mysteriously aborting builder’ problem. If I run into it again, I’ll definitely want to do some more digging as to why it was happening.

Wrap-Up

Hopefully this helps you better integrate Eclipse and Ant, and become a more efficient developer :)

Related Reading:

Matt Raible covered this a while ago in the context of AppFuse.

ant4eclipse allows even tighter integration between Eclipse and Ant. I haven’t used it, but it looks promising.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>