how to create java document using ant in eclipse?

As we discussed all basic concepts of Apache ant, let us see the below example to create java project document using ant.

In build.xml file:

1. src.dir: It specify the project source folder.
2. build.dir: It specify the project compilation output folder.
3. docs.dir: It specify the project document output folder.

Example explanation:

When we run the build.xml file, control first go on the project element and look for the default target. In our example mainTarget is the default target so control go on the mainTarget. Then control looks for those targets on which mainTarget depends. The mainTarget depends on compile and docs target. The docs target depends on compile target which is further depends upon clean and makedir target. So first clean target will execute followed by makedir, compile, docs and mainTarget. The javadoc task in the docs target is responsible for documentation.

Example:

ArmstrongNumber.java

/**
 * This program is used to find that given number is Armstrong or not.
 * @author w3schools
 */
public class ArmstrongNumber {
	/**
	 * This method is used to find that 
         * given number is Armstrong or not.
	 * @param num
	 */
	static void armstrong(int num){
		int newNum = 0, reminder, temp;
		temp = num;
		//find sum of all digit's cube of the number.
		while(temp != 0){
			reminder = temp % 10;
			newNum = newNum + reminder*reminder*reminder;
			temp = temp/10;
		}
		//Check if sum of all digit's cube of the number is 
		//equal to the given number or not.
		if(newNum == num){
		  System.out.println("Given no. is an armstrong no.");
		}else{
		  System.out.println("Given no. is not an armstrong no.");
		}
	}
 
	public static void main(String args[]){
		//method call
		armstrong(407);
 
	}
}

build.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!-- Set the project name, basedir and default target to be executed-->
<project name="Ant-Build-Document-Test" default="mainTarget" basedir=".">
  <!-- Sets the properties here-->
  <property name="src.dir" location="src" />
  <property name="build.dir" location="bin" />
  <property name="docs.dir" location="docs" />
 
  <!-- Target for deleting the existing directories-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
  </target>
 
  <!-- Target for creating the new directories-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
  </target>
 
  <!-- Target for compiling the java code-->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
    </javac>	
 
  </target>
 
  <!-- Target for creating the Java documentation-->
  <target name="docs" depends="compile">
    <javadoc packagenames="src" 
    	sourcepath="${src.dir}" destdir="${docs.dir}">
      <!-- Task for including files-->
       <fileset dir="${src.dir}">
             <include name="**" />
       </fileset>
    </javadoc>
  </target>
 
  <!-- Defualt target to run all targets-->
  <target name="mainTarget" depends="compile, docs">
    <description>Main target</description>
  </target>
 
</project>

Console:

Buildfile: D:\TestWorkspace\AntBuildDocument\build.xml
clean:
   [delete] Deleting directory D:\TestWorkspace\AntBuildDocument\bin
   [delete] Deleting directory D:\TestWorkspace\AntBuildDocument\docs
makedir:
    [mkdir] Created dir: D:\TestWorkspace\AntBuildDocument\bin
    [mkdir] Created dir: D:\TestWorkspace\AntBuildDocument\docs
compile:
    [javac] D:\TestWorkspace\AntBuildDocument\build.xml:23: 
    warning: 'includeantruntime' was not set, defaulting to 
    build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to 
    D:\TestWorkspace\AntBuildDocument\bin
docs:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source file 
  D:\TestWorkspace\AntBuildDocument\src\com\w3schools\business\ArmstrongNumber.java...
  [javadoc] Constructing Javadoc information...
  [javadoc] Standard Doclet version 1.6.0_13
  [javadoc] Building tree for all the packages and classes...
  [javadoc] Building index for all the packages and classes...
  [javadoc] Building index for all classes...
mainTarget:
BUILD SUCCESSFUL
Total time: 2 seconds

Download this example.
 
Next Topic: How to use classpath in ant with example?
Previous Topic: How to build java project using ant in eclipse with example?