Apache Ant: Learn Apache Ant from IBM Experts

learn-apache-ant-from-experts

Section 3. Running Ant

Running Ant introduction

The Apache Ant tool can be invoked in various ways. On its own, Ant comes in command-line form, and is usually invoked from a UNIX or Linux shell prompt or a Windows command prompt, with the build files being written using your text editor of choice. This is fine if the project to be built is being developed in this way, but many find IDEs more convenient. Most of these offer some level of support for Ant, so at a minimum Ant builds can be invoked without the hassle of having to leave the IDE to perform command-line operations.

In this section, we’ll see how to use Ant from the command line, and learn some useful command-line options. Then we’ll take a brief tour of the Ant support offered by the open source Eclipse platform. (You should be at least passingly familiar with Eclipse in order to get the most out of those panels.)

Running Ant from the command line

Invoking Ant from your command prompt can be as simple as just typing ant on its own. If you do so, Ant uses the default build file; the default target specified in that build file is the one that Ant attempts to build. It is also possible to specify a number of command-line options, followed by any number of build targets, and Ant will build each of these targets in order, resolving any dependencies along the way.

Here is some typical output from a simple Ant build executed from the command line:

Buildfile: build.xml

init:
[mkdir] Created dir: E:\tutorials\ant\example\build
[mkdir] Created dir: E:\tutorials\ant\example\dist

compile:
[javac] Compiling 8 source files to E:\tutorials\ant\example\build

dist:
[jar] Building jar: E:\tutorials\ant\example\dist\example.jar

BUILD SUCCESSFUL
Total time: 2 seconds

We’ll see just what all that means as we move on through the tutorial.

Command-line options

Just as the make tool looks for a build file with the name makefile by default, Ant looks for a file with the name build.xml. Therefore, if your build file uses this name, you don’t need to specify it on the command line. Naturally, it is sometimes convenient to have build files with other names, in which case you need to use the -buildfile arguments to Ant ( -f is the abbreviated version).

Another useful option is -D, which is used to set properties that can then be used in the build file. This is very useful for configuring the build you want to initiate in some way. For example, to set the name property to a particular value, you would use an option that looks something like this:

-Dmetal=beryllium
This capability can be used to override initial property settings in the build file. As noted earlier, you cannot change the value of a property once it has been set. The -D flag sets a property before any information in the build file is read; because the assignment in the build file comes after this initial assignment, it therefore doesn’t change the value.

IDE integration

Due to Ant’s popularity, most modern IDEs now have integrated support for it, and many others offer support for it in plugins. The list of supported environments includes the JEdit and Jext editors, Borland JBuilder, IntelliJ IDEA, the Java Development Environment for Emacs (JDEE), NetBeans IDE, Eclipse, and WebSphere (R) Studio Application Developer.

Eclipse support for Ant

The open source Eclipse project offers a great deal of support for Ant. The core of this support is Eclipse’s Ant editor, which features syntax highlighting. The editor is illustrated below:

<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>

This editor provides content assistance — for example, typing tag, and a short explanation of that task.

You can also see an outline view that shows the structure of the build file and provides a quick way of navigating through the file. There is also an Ant view that allows targets to be built from a number of different Ant files.

Running Ant builds from within Eclipse

Files named build.xml are identified and decorated in Eclipse’s navigator view with an Ant icon. (See File associations in Eclipse if your build file has a different filename.) Right-clicking these files provides a Run Ant… menu option that opens a dialog like the one shown below:

<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>

All of the targets from the build file are shown, with the defaults selected. After you’ve decided whether or not to change the default targets, press the Run button to launch Ant. Eclipse will switch to the Console view to show the output, as illustrated below. Errors are shown in a different color, and you can click on task names in the output to jump to the corresponding invocation in the build file.

<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>

File associations in Eclipse

By default, Eclipse only uses the Ant editor on files named build.xml, but you can easily configure the editor to recognize files with other names. Select Window=>Preferences from the menu, then expand the Workbench group, and select the File Associations preference page. Then add a new file type for the desired filenames. You could, for example, add a new file type for all files named mybuild.xml. You could even use *.xml if you want to do the same for all files with an .xml suffix (except for specific filenames, such as plugin.xml, which in Eclipse override the wildcard specifications). Finally, add an associated editor for this new file type, and select Ant editor from the editor selection list, as shown below:

<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>

Tagged : / / / / / / / / / / / / / / / / / / / / / / /

Leave a Reply