An Example Through Analysis: The jar Task

Now that the theoretical stuff is behind us, let’s look at what happens when rubber meets the road. To develop your own Ant task, write a Java class that implements your design. The complexity or simplicity of your task is up to you. It’s only important that your Java class conform to the conventions set forth in Ant’s object model.

As an example of how to write tasks, we present an analysis of an existing task: jar. The jar task touches upon all of the topics we need to cover. The Jar task class is part of a deep hierarchy, demonstrating re-use through inheritance. It derives from Zip, which, in turn, derives from MatchingTask. The Jar task object does not have its own execute( ) method implementation, relying, instead, on that method’s implementation in the Zip classes. This shows how loose some of the requirements are in regards to your own implementations. The jar task also uses a multitude of attributes and nested elements, giving us good examples of how to handle all of these features. Using an existing task as an example reinforces the concept that there is no difference between user-written tasks and those included with the Ant distribution.

Analyzing jar gives us some insight into how to design a task. It has unique and easy-to-understand design goals. We have a task design with object re-use that is open for future extension. War and Ear derive from Jar, obtaining the same benefits. However, we will not cover every feature and ...

Get Ant: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.