Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code
more resources

Keep the Ant, Hold the XML
Employ Ant programmatically in Java code without the constraints of XML for easier customization
by Kei G. Gauthier

Posted May 31, 2004

Over the past few years, I have become dependent on Apache Ant to manage the process of building my Java projects. I find this tool indispensable when I need to package large amounts of code and other resources into an easily deployed archive. No tool has come to my attention that comes close to Ant's cross-platform ability to search directories for files that match specific patterns, compile these files, and accumulate the results into an efficient archive.

As my projects have become more intricate, however, the complexity of translating the logic of my builds into XML has become daunting. I still need the Ant classes, but I require more control over program flow and build customization than Ant offers.

ADVERTISEMENT

The difficulties of coding builds in XML finally overwhelmed me when I started experimenting with the creation of native Java executables that use the Eclipse Standard Widgets Toolkit as a graphical user interface. This can be done with the GNU GCJ Java compiler. To create such programs requires natively compiling resources such as image files and Java source code and then linking the resulting output together with a static library into an executable file. Even with elegantly written XML builds available to use as models, I could not create the necessary XML tags to perform my own builds.

Reviewing my failed efforts, I found that all I really wanted was code that branched and looped, true variables, and maybe the ability to dump the entire process into a GUI interface. This I could not do in XML, though I still wanted to use Ant for its many strengths. I found, however, that I could have everything that I wanted and more by accessing Ant programmatically within Java code and avoiding XML altogether.

Using Ant Programmatically
Incorporating Ant classes into your Java code gives you more control by avoiding a layer of translation. Ant consists of Java classes. To design a build in XML, you write tags that are recast into Java to run the methods of these classes. Writing in Java lets you better manage your build by avoiding this layer of translation. You can construct exactly the classes you want and call only those methods that you need.

Although the Ant XML definition lets you affect program flow in some ways, this control pales when compared to what is available in Java. Calling Ant tasks directly in Java permits access to the full range of Java branching and looping instructions to manage program execution.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home