|
3.5. Documenting CodeCreating applications in a commercial environment frequently means creating documentation, and Ant is up for that with the javadoc task. This task creates documentation using the Java javadoc tool, a process that involves compilation, which merits including this task in this chapter. This is one of the largest tasks in Ant because of the enormous number of javadoc options. Here's an example, stored in the javadoc folder in the code for this book. Suppose you add a documentation-type comment to the Project.java file: /** This application prints out "No worries." */ public class Project { public static void main(String args[]) { System.out.println("No worries."); } } You can create Javadoc for the project using the javadoc task in a new target I'll name doc, which will put the generated Javadoc in a doc directory, as you see in Example 3-3.
Example 3-3. Creating javadoc (ch03/javadoc/build.xml)<?xml version="1.0" ?> <project default="main"> <property name="message" value="Building the .jar file." /> <property name="src" location="source" /> <property name="docs" location="docs" /> <property name="output" location="bin" /> <target name="main" depends="init, compile, doc, compress"> <echo> ${message} </echo> </target> <target name="init"> <mkdir dir="${output}" /> <mkdir dir="${docs}" /> </target> <target name="compile"> <javac srcdir="${src}" destdir="${output}" /> </target> <target name="doc"> <javadoc sourcefiles="${src}/Project.java" destdir="${docs}" author="true" version="true" use="true" windowtitle="Project API"> <doctitle><![CDATA[<h1>Project API</h1>]]></doctitle> <bottom><![CDATA[<i>Copyright © 2005</i>]]></bottom> </javadoc> </target> <target name="compress"> <jar destfile="${output}/Project.jar" basedir="${output}" includes="*.class" /> </target> </project> You can see the javadoc task at work when the build file runs: %ant Buildfile: build.xml init: [mkdir] Created dir: /home/steven//ch03/javadoc/bin [mkdir] Created dir: /home/steven//ch03/javadoc/docs compile: [javac] Compiling 1 source file to /home/steven//ch03/javadoc/bin doc: [javadoc] Generating Javadoc [javadoc] Javadoc execution [javadoc] Loading source file /home/steven//ch03/javadoc/source/Project.java... [javadoc] Constructing Javadoc information... [javadoc] Standard Doclet version 1.4.0 [javadoc] Building tree for all the packages and classes... [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes... [javadoc] Generating /home/steven//ch03/javadoc/docs/stylesheet.css... compress: [jar] Building jar: /home/steven//ch03/javadoc/bin/Project.jar main: [echo] [echo] Building the .jar file. [echo] BUILD SUCCESSFUL Total time: 10 seconds And you can see the index.html Javadoc result in Figure 3-1. Note the message "This application prints out `No worries.'" Figure 3-1. New Javadoc for the project![]() You can see the huge number of attributes of this task in Table 3-9.
Here's another example. This one passes Java packages starting with gui to javadoc to document them, excludes a few specific packages, sets the header text for each page, groups the packages gui.steve.api.* together on the first page under the title "Group 1 Packages," and includes a link to the Java 1.4.2 docs: <javadoc destdir="api" version="true"> <packageset dir="code"> <include name="gui/**" /> <exclude name="gui/debug/**"/> </packageset> <header><![CDATA[Preliminary API Specification]]></header> <doctitle><![CDATA[<h1>Test</h1>]]></doctitle> <group title="Group 1 Packages" packages="gui.steve.api.*"/> <link href="http://java.sun.com/j2se/1.4.2/docs/api/"/> </javadoc> |
|