RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Visualizing Data: Self-Documenting Ant files : Page 3

Interactive graphical views of your Ant build files give you more information faster, reduce errors, and provide intuitive methods to help you comprehend, modify, and test build targets.

Ant's Intrinsic Help Facility
Yet another approach that takes virtually no installation is the -projecthelp option of Ant itself.

Ant's Intrinsic Help Facility

Ease of Installation5
  • No installation required; you use the built-in command-line option.
  • To force "help" to be the default, just insert the help target code fragment and set the project default to that target.
Ease of Use5Invoke Ant with command-line flag -projecthelp or with no arguments.
Shows Targets4
  • Lists targets with descriptions, but no source.
  • Displays only public ones; keeps private ones private.
Shows Connections1 
Strengths Exposes only public targets; good API summary.
Weaknesses Shows no relationships, ordering, or source.
Sample file build-help.xml
Download —none—

Figure 6. Ant's Built-In Help: The "-projecthelp" command-line option reveals the public API of your build file.
Figure 6 shows the output for the build file in Listing 1, which displays the project description, all the public targets, and the default target. This is very basic output, of course, but it does two things very well: It presents a summary of what the build file does, and it shows only the public API.

Not satisfied to leave well enough alone, however, I prefer the convention of many command-line tools, which is that when you don't provide any arguments, the command simply describes its own usage and nothing else. With Ant, you could certainly argue that you want your build file to default to a specified action (as you'll see in several examples shortly) but I think it's useful to learn how to make help the default because this tip is rather obscure to track down if and when you need it.

The sample build file shows how you can make the help target the default. Simply set the default attribute to the help target and define the target as shown. (I have called it "help" but you are free to name it anything as long as you update both the target name and its reference.)

   <project name="MyProject" default="help" basedir=".">
           simple example build file
     <!-- set global properties for this build -->
     <property name="src" location="src"/>
     <property name="build" location="build"/>
     <property name="dist"  location="dist"/>
       <target name="help" 
         description="a wee bit of help">
Figure 7. Help Target Output: After you instrument the code with a "help" target, launching Ant with no arguments gives you the same result as using the "-projecthelp" argument.
<java classname= "org.apache.tools.ant.Main"> <arg value="-buildfile"/> <arg value="${ant.file}"/> <arg value="-projecthelp"/> </java> </target> . . . </project>
With this target installed in your build file, you can then invoke the build file with no arguments and get the same output that the -projecthelp argument would return, as shown in Figure 7.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date