Ant make directory with relative path

This example illustrates how to make directory, how to compile java file and how to create jar file.

Ant make directory with relative path

Ant make directory with relative path

     

This example illustrates how to make directory, how to compile java file and how to create jar file. This is a simple program that uses <classpath refid="test.classpath"> to map with the jar file. In this example five targets are used, the first target <target name="clean"> is used to delete the build and the dist directory. The second target <target name="prepare"> is used to create the build and the dist directory. The third target <target name="compile"> is used to compile the java file and copy the class file in build directory. The fourth target <target name="jar"> is used to create the jar file in the dist directory from the name of test.jar. The fifth target <target name="test"> is used to map with the class path by the reference id. The source code of build.xml file is as follows: 

 

 

 

<project name="AntPath" default="test" basedir=".">
  
  <property name="class" value="Test"/>
  
  <path id="test.classpath">  
  <pathelement location="dist/test.jar"/>
  </path>

  <target name="clean">
  <delete dir="build"/>
  <delete dir="dist"/>
  </target>

  <target name="prepare" depends="clean">
  <mkdir dir="build"/>
  <mkdir dir="dist"/>
  </target>
  
  <target name="compile" depends="prepare">  
  <javac destdir="build" debug="on" optimize="on">
  <src path="src"/>
  </javac>
  </target>
  
  <target name="jar" depends="compile">
  <jar jarfile="dist/test.jar">
  <fileset dir="build">
  <include name="test/*.class"/>
  </fileset>
  </jar>
  </target>

  <target name="test" depends="jar">
  <java fork="true" failonerror="no" classname="${class}">
  <classpath refid="test.classpath"/> 
  <arg line=""/>
  </java>
  </target>

</project>


 Source code of Test.java:

class Test{
  public static void main(String args[]){
  System.out.println("RoseIndia Technology Pvt. Ltd.");
  }
}


Run this program on the appropriate path with ant command. The following output will be displayed.



Download Source Code