First and foremost, the build process must produce consistent results, but efficient use of developers' and builders' time is important too.
Go into this directory. Select these source files. Right-click here. Export this. Rename that. Copy the renamed file there. Does this sound familiar? Yes! It is your build process! Wouldn't it be nice if you could (conveniently) build your Java project with a single mouse-click or a single command? The good news is that you can--with the Apache Software Foundation's Ant tool (named "Ant" by its creator, James Duncan Davidson, because it's a little thing that can build big things).
Importance of an Automated Build Process
Before delving into the features of Ant, I'd like to briefly reflect on the importance of an easy-to-use build process. First and foremost, the build process must produce consistent results. Imagine a project in which each developer produces "test builds" in some distinct fashion. That is, each developer is effectively testing "different" products in that no two build artifacts (e.g., class files, jar files, etc.) were produced in like fashions. This can have huge quality impacts. For example, imagine the following hypothetical situation:
A company produces a product that must run in JDK 1.4 and later JVMs. One of the developers writes his source code such that it contains a reference to an API that was introduced in JDK 1.5. As the developer tests his code, he forgets to compile his source code with the -source 1.4 compiler flag, and the JDK incompatibility remains unnoticed, and the developer checks in his "working" source code. That's a ticking time bomb waiting to blow!
As you can see from the above example, subtle bugs can be easily introduced in the absence of a consistent build mechanism, both for test and production environments.
Similarly, the build utility should be relatively simple to use. The risk of user error grows with each additional manual step, so simplicity is crucial to error-free builds. The simplicity factor also heavily contributes to more efficient use of developers' and builders' time. The less time these folks need to spend fiddling with the quirks of building, the more time that they can spend coding!
General Ant Features
Building!
In the beginning, build tools were highly shell-based and tightly coupled to the environment in which the build occurred. As a solution to this problem, Ant was developed. An Ant script is nothing more than an XML file (typically named build.xml) that specifies the tasks (i.e., an object that implements a Task interface) available for execution. An Ant task is merely a logical instruction for Ant to perform; each task can explicitly call out other task dependencies (e.g., before task B can execute, task A must execute). For example, javac is a task that can be used to compile Java source code. Since Ant's inception, the number of tasks that are supported out-of-box have dramatically increased, thus making even the most complex builds feasible. Click here to view a complete list of tasks supported out-of-box; there's everything from archival tasks to Junit testing tasks.
General Scripting
Because the tasks that ship with Ant are so feature-rich (again, refer to this link for the set of available Ant tasks), many people use Ant scripts not only to build projects, but as a platform-independent scripting language. With Ant, you can copy files, rename files, compress files, perform search/replace operations, and the list goes on and on; this often makes Ant a good substitute for creating and maintaining platform-specific scripts (e.g., .bat files on Windows and .sh scripts on Linux).
Seamless Eclipse Integration
Ant is seamlessly integrated into the Eclipse Integrated Development Environment (IDE); it's highly recommended to use Eclipse 3.2 or later. Within Eclipse, you can access the Ant view by clicking Window > Show View > Ant. You can then simply drag and drop your build.xml file into the Ant view's window and you'll be presented with a set of targets to run (i.e., by double-clicking the desired target), as shown below in Figure 1.
Figure 1: Eclipse's Ant View gives you a set of targets to run. (Click image to enlarge.)
This tight integration makes it easy to run Ant tasks from within your development environment, something every developer is fond of. Also note that for those folks who don't use Eclipse or prefer to work in a headless environment (e.g., Linux command line), Ant also has a simple-to-use command line interface.
Looking Ahead
Hopefully, I've whet your appetite with some of the (time-saving) features that Ant has to offer. In next week's TechTip, I'll demonstrate using Ant to build a simple "Hello World" Java project.
LATEST COMMENTS
MC Press Online