Section 10.4. Handling Dependencies

10.4. Handling Dependencies

The javac task does a good job of handling dependencies, but it's limited because it only compiles .java files if the corresponding .class file is older or does not exist. It doesn't check the files those .java files might depend on, such as parent or imported classes. The depend task, however, lets you perform this kind of dependency checking.

When this task finds out-of-date classes, it removes the .class files of any other classes that depend on them. To determine dependencies, this task analyzes the classes in all files passed to it, using the class references encoded into .class files by the compiler. (It does not parse or read source code.)

You typically use the depend task before compiling. Here's an example that uses depend before calling javac:

<?xml version="1.0" ?> <project default="main">     <property name="message" value="Building..." />     <property name="src" location="source" />     <property name="output" location="bin" />     <target name="main" depends="init, compile, compress">         <echo>             ${message}         </echo>     </target>        <target name="init">         <mkdir dir="${output}" />     </target>        <target name="compile">         <depend srcdir="${src}"             destdir="${output}"             closure="yes"/>         <javac srcdir="${src}" destdir="${output}" />     </target>      <target name="compress">         <jar destfile="${output}/Project.jar" basedir="${output}"              includes="*.class" />   </target> </project>

The attributes of this task appear in Table 10-4.

If you don't want to have to check dependencies, you can wipe all the directories that contain compiled code and rebuild from scratch. When there are a large number of files to compile, that's a less attractive option, and using the depend task can save significant time. (But experience shows that if your dependencies are complex, it can save time to do a wipe and start fresh.)

Table 10-4. The depend attributes






Specifies the directory where you want the task to store dependency data




Specifies the classpath, which should also be checked when checking dependencies




Specifies the task should traverse all class dependencies, deleting all classes that depend on out-of-date material




Specifies the root directory containing the class files that you want to check


The value of srcdir


Specifies the dependency information should be sent to the debug log




Specifies the directory where the source is



Like many other Ant tasks, this task forms an implicit FileSet and so supports all attributes of fileset (though dir becomes srcdir) as well as the nested include, exclude, and patternset elements. The depend task's classpath attribute is a path-like structure and can also be set using a nested classpath element. If you specify a classpath, depend will include classes and JARs on the classpath for dependency checking; any classes which depend on an item from the classpath and which are older than that item will be deleted.

If you're going to include a classpath to check for dependencies, don't end up including the entire Java library structure by mistake; doing so will slow this task down.

Checking dependencies can become involved. For example, what if a class depends on another, which in turn depends on a third? If the third class is out of date, only the second class would normally be rebuilt, even if you use the depend task. But you can ensure the depend task catches all dependencies, including indirect ones like this, by setting the closure attribute to true.

Nonpublic classes can also cause a couple of problems with this task. For example, depend cannot connect the class file containing a non-public class to a source file. depend can't detect when a nonpublic class is missing. If you've set the Java compiler to optimize its compilation, that can also cause problems. Inner classes, on the other hand, are no problem.

Normal Ant processing usually handles dependencies with no problem, but if you've got a complex dependency situation, or indirect dependencies, this task can work wonders. If you can't handle your dependencies with depend, wipe the output directories before compiling to start with a clean slate.

    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: