ant2dot.xsl
ANT script visualization

  SourceForge.net Logo
http://sourceforge.net/projects/ant2dot/

Table of Contents

 
What?
How?
Download
Quick Start
Examples
Features
Options
FAQ
To Do
Version History
Miscellaneous
Acknowledgements
Mailing Lists
[showcase graph]
 

What?

The ant2dot.xsl stylesheet provides a means to visualize the targets in an ANT build script and their dependencies.

How?

More specifically, ant2dot.xsl is an XSL stylesheet that transforms an ANT build script (written in XML) into a DOT file, which in turn can be transformed into a unidirected graph (as a GIF, JPG or PNG image) using the GraphViz toolkit.

Download

The goodies you want

Use "Save target as..." (or "Save link as..." depending which browser you are using) to download the ant2dot.xsl stylesheet onto your disk.

A good place to save the stylesheet to is the ${ANT_HOME}/etc directory (this is where the Ant distro keeps a bunch of stylesheets used by some of the core and optional tasks).

Other stuff you need

To use the ant2dot.xsl stylesheet you will need a XSLT processor (like Xalan) and the GraphViz toolkit.

Note that JDK 1.4.x includes the Xalan XSLT processor, so if you're using this version of Java, you only need to download and install the GraphViz toolkit.

Also note that Pixelglow Software maintains a superb Mac OS X port of the Graphviz toolkit.

The Quick Start section of this document describes a number of ways to generate the unidirected graph using the stylesheet.

Quick Start

There are two ways to generate the image containing the unidirected graph: the first one generates the image from the command line, while the second one generates the image using an ANT target. As mentioned in the Download section, you will need to download and install an XSLT processor and the GraphViz toolkit onto your system for all this to work.

From the command line

First, the DOT file needs to be generated.

If you are using JDK1.3.x or lower, download and install the Xalan XSLT processor onto your system. Assuming xalan.jar and ant2dot.xsl have been copied into the directory containing the ANT script you want to visualize, the DOT file can be generated using the Xalan processor as follows:

  > java -cp xalan.jar org.apache.xalan.xslt.Process
         -IN build.xml
         -XSL ant2dot.xsl
         -OUT build.dot

If you are using JDK1.4.x you already have a copy of the Xalan processor on your system, and the following command can be used to generate the DOT file:

  > java org.apache.xalan.xslt.Process
         -IN build.xml
         -XSL ant2dot.xsl
         -OUT build.dot

The above command instructs the Xalan processor to apply the ant2dot.xsl stylesheet to the build.xml ANT script, and to write the output to the build.dot DOT file.

Next, the unidirected graph needs to be generated.

To do this, download and install the GraphViz toolkit onto your system. Assuming the dot(.exe) executable can be found on your PATH, the unidirected graph can be generated using the GraphViz dot utility as follows:

  > dot -Tpng build.dot -o build.png

The above command instructs the dot utility to generate the unidirected graph into the build.png image using the build.dot DOT file generated in the first step as input. You can now open and view build.png using your preferred image viewer. Other image formats are supported by the dot utility, and can be generated as follows:

  > dot -Tgif build.dot -o build.gif
  > dot -Tjpg build.dot -o build.jpg

No matter the output format, you always use the same build.dot input file.

Using an ANT target

If your version of ANT supports the XSLT task, and a compatible XSLT processor can be found in ANT's sysclasspath (which will be the case if either you use JDK1.4.x or if you copy xalan.jar to ${ANT_HOME}/lib on Unix platforms or %ANT_HOME%\lib on Windoze), then you can visualize any ANT build script by adding the following target to it:

  <target name="ant2dot">

    <!-- STEP 1: set some ANT properties -->
    <property name="build.xml" value="${ant.file}"/><!-- ie. this ANT script -->
    <property name="ant2dot.xsl" value="${basedir}/ant2dot.xsl"/><!-- the stylesheet -->
    <basename property="basename.script" file="${build.xml}" suffix=".xml"/>
    <property name="dot.file" value="${basename.script}.dot"/>
    <property name="png.file" value="${basename.script}.png"/>

    <!-- STEP 2: generate the DOT file ("build.dot" or something similar) -->
    <xslt in="${build.xml}" out="${dot.file}" style="${ant2dot.xsl}" force="true" />

    <!-- STEP 3: generate the PNG file ("build.png" or something similar) -->
    <exec executable="dot.exe" dir="${basedir}" os="Windows 2000">
      <arg value="-Tpng"/>
      <arg value="${dot.file}"/>
      <arg value="-o"/>
      <arg value="${png.file}"/>
    </exec>

  </target>

This target makes the same assumptions as the command line approach, namely that the GraphViz toolkit is installed on your system and that the dot(.exe) executable can be found on your PATH. To generate the build.dot and the build.png files, use the following ANT command:

  > ant -buildfile build.xml ant2dot

The above command invokes the ant2dot target, which generates the unidirected graph into the build.png image using the build.dot DOT file generated in the second step as input. You can now open and view build.png using your preferred image viewer.

Examples

The table below shows an example ANT script with targets and dependencies that are quite typical for a Java project. The corresponding unidirected graph consists of nodes that correspond to ANT targets and edges that correspond to target dependencies.

ANT script
<project name="simple" default="junit" basedir=".">

    <target name="init"/>
    <target name="clean" depends="init"/>
    <target name="compile" depends="init"/>
    <target name="junit" depends="compile"/>
    <target name="javadoc" depends="compile"/>
    <target name="install" depends="junit, javadoc"/>

</project>
graph
[simple graph]

The next table shows an ANT script in which some of the targets use the ant and antcall tasks. The antcall tasks are visualized using a dashed edge between the two nodes involved. The ant tasks are also visualized using a dashed edge; and in addition the external target is visualized as a record node, containing not only the target name, but also the directory and file name of the external ANT script.

ANT script
<project name="complex" default="deploy" basedir=".">

    <target name="init"/>
    <target name="clean" depends="init"/>
    <target name="junit">
        <ant antfile="simple.xml" target="junit"/>
    </target>
    <target name="javadoc">
        <ant antfile="simple.xml" target="javadoc"/>
    </target>
    <target name="deploy" depends="junit, javadoc">
        <antcall target="clean"/>
    </target>

</project>
graph
[complex graph]

The Options section below has other examples illustrating the various parameters that can be passed into the stylesheet.

Features

ANT-related features

XSLT-related features

DOT-related features

Options

The following examples illustrate some of the more advanced options supported by the stylesheet; all examples are based on the same ANT build script used in the Examples section above, but by passing one of the supported parameters into the stylesheet, a slightly different graph is generated in each case. The following stylesheet parameters are currently supported:

graph.label
results in a border around the graph, with the value of the graph.label parameter used to generate a label below the graph
create.project.node
results in a record node on the graph containing basic information about the ANT project taken from the ANT build script
use.target.description
results in target nodes containing not only the target name, but also the target description (if present in the build scripts)
use.target.conditions
results in target nodes containing the "if" and "unless" target conditions specified in the ANT build script

When using the command line approach for generating the DOT file, you pass in an XSL parameter as follows:

  > java org.apache.xalan.xslt.Process
         -PARAM graph.label simple.xml
         -IN build.xml
         -XSL ant2dot.xsl
         -OUT build.dot

When using the ANT target approach for generating the DOT file, you pass in an XSL parameter as follows:

  ...

    <!-- STEP 2: generate the DOT file ("build.dot" or something similar) -->
    <xslt in="${build.xml}" out="${dot.file}" style="${ant2dot.xsl}" force="true">
        <param name="graph.label" expression="simple.xml"/>
    </xslt>

  ...

These stylesheet parameters are illustrated below; note that these parameters can be combined at random.

parameter graph
<no options>
  [simple graph]
-PARAM graph.label simple.xml
<param name="graph.label" expression="simple.xml"/>
  [simple graph]
-PARAM create.project.node true
<param name="create.project.node" expression="true"/>
  [project graph]
-PARAM use.target.description true
<param name="use.target.description" expression="true"/>
  [description graph]
-PARAM use.target.conditions true
<param name="use.target.conditions" expression="true"/>
  [if/unless graph]

FAQ

Q. dot(.exe) issues the following warning: Could not find/open font : Courier in <some path>

This warning message indicates that dot(.exe) cannot locate the descriptor file for the Courier font (which is the font specified in the ant2dot.xsl stylesheet); on Windows, this message can be ignored, as dot(.exe) will use its default font instead; on Solaris (and possibly other *nix platforms) this warning can be fixed by making sure the DOTFONTPATH environment variable correctly includes the platform-specific fonts directory; see the GRAPHVIZ FAQ for more info.

To Do

Version History

Miscellaneous

Coming soon...

Acknowledgements

The following people have adopted and tested ant2dot.xsl, provided valuable feedback, and suggested useful improvements which have been incorporated already or else put on the To Do list: Gary Frost, David Gardner, Johannes Schaback, Ulrich Shäfer.

Tilaye Y Alemu has developed an Eclipse plugin that uses ant2dot.xsl to visualize ANT build scripts within the Eclipse IDE... head over to http://eclipseant2dot.ertale.com/ to download the goodies!

"A great idea is usually original to more than one discoverer."
(Elizabeth Stuart Phelps)

As it turns out, I wasn't the first person to consider the idea of transforming an Ant build script into a DOT file by means of an XSL stylesheet: long before I put ant2dot.xsl on SF, Stefan Kost posted a similar stylesheet with the same name to the ANT-USER mailing list ("xsl script to generate graphical representation of build.xml"), and Vadim Nasardinov put yet another but also similar stylesheet with the same name up on the RedHat website ("a result of my semi-annual exercise in XSLT programming").

Another ANT visualization framework is available from www.softwaresecretweapons.com; on this site you can generate a diagrammatic representation of your ANT build script on-line!

Mailing Lists

Two mailing lists have been set up: new releases of the ant2dot.xsl stylesheet will be announced on ant2dot-announce, and general discussions of the stylesheet can be posted to ant2dot-discuss. You can subscribe to one or both of these mailing lists using the following forms:

Subscribe to ant2dot-announce
Powered by groups.yahoo.com
Subscribe to ant2dot-discuss
Powered by groups.yahoo.com