Working With ANT

ANT is a Java build tool. It is very easy to use ANT in Eclipse. For example, you have a simple Java project with following code:

public class HelloWorld {
 
public static void main(){
 
 	System.out.println("Hi");
 
 }
 
public void hi()
 
 {
 
 	System.out.println("Hi");
 
 }
 
}

And you want to compile it and place the class file into the build folder. This can be done using ANT. To create and ANT file, follow these steps In Eclipse IDE:

  1. ” File ” – > ” New ” – > ” File ” – > filename.xml.Extension of file should be xml.
  2. Change the default editor for a XML file to the Ant Editor. This can be done as follows:
    • ” Window ” – > ” Preferences ” – > ” Workbench ” – > ” File Associations “.
    • Click ” Add ” to add a new extension, type *.xml and click OK.
    • Select it from File Types box, and in the Associated Editors box click ” Add “.
    • Select Ant Editor.
  3. Now in the ANT file, paste following code:
  4. <project name="test" default="compile" basedir=".">
    <property name="src" value="."></property>
    <property name="build" value="build"></property>
     
         <target name="init">
              <mkdir dir="${build}">
         </mkdir>
         <target name="compile" depends="init">
              <javac srcdir="${src}" destdir="${build}">
         </javac>
    </target></target></project>
  5. Right click the file and click “Open with ” – > ” Ant Editor “.
  6. You should make Outline view visible. This can help you in writing ANT files. This can be done as follows:

  7. ” Window ” – > ” Show View ” – > ” Outline “.
  8. You will notice that there will be entries for each property and each target.

  9. Now we have everything set. We will now run ANT file. Select ANT file from the Navigator and choose ” Run ” – > ” External Tools ” from its context menu. The External Tools dialog will appears. Click ” New ” to create a new configuration. Make the required settings and click ” Run “. The Ant buildfile will run and you can check the console.