|
10.4. Handling DependenciesThe 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.
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.
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.
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. |
|