Home > Application Development, Java > Two Simple Ant build Tricks

Two Simple Ant build Tricks

November 26th, 2006

h3. Ant build scripts and NetBeans

Here is a small trick for working with NetBeans and Ant build files. One problem with NetBeans is that if your source code already has a build file, NetBeans will use that build file. Unfortunately, when you do this, you loose a fair bit of NetBeans functionality like the ability to run individual JUnit test classes. You can test the entire application, assuming that you have setup a JUnit test target in your build file, but you cannot run a single test. This little trick gets around that.

I have to start by saying that this trick is fairly obvious. The trick is to rename the build file. Instead of calling it @build.xml@, call it something else. This will allow you to setup your project, in NetBeans, as a “project With Existing Sources” instead of a “With Existing Build Script”. This means that NetBeans will create its own build script and give you the full functionality and power of NetBeans.

h3. Refactoring Build Scripts

If you have an application that is divided into several different projects that you would like to manage with separate build scripts, but not necessarily different build scripts, then this trick will work. The idea is to use the “DRY(Don’t Repeat Yourself)”:http://en.wikipedia.org/wiki/Don’t_repeat_yourself principle. Take all the parts that are the same in the build scripts and put them into a separate file. Then import that file into the build scripts for each project.

The advantage is now when you have to make changes, you only need to go to one file. You no longer need to edit one file and then copy-paste those changes into several more files. This saves you from copy-paste errors or forgetting to change a file.

Categories: Application Development, Java Tags:
Comments are closed.