Apache Ant (Another Neat Tool) is a Java library and Command line utility useful for automating batch code build process.
For example, let say there is Java Class file HelloWorld.java you need to compile, jar, move and run. In command you will perform the following activities
To Compile
javac HelloWorld.java
To Create the jar file
jar -cvf HelloWorld.jar HelloWorld.class
or
jar -cvf HelloWorld.jar HelloWorld.class jar -cvf HelloWorld.jar HelloWorld.java
To Move the program
move HelloWorld.jar HelloWorld1.jar
To run the program
java -cp HelloWorld.jar HelloWorld
Executing all the above steps is ok for one time, lets say you have to do 100 times in a month. This is troublesome and can be automated.
Lets see how this can be automated
Step 1: Download the Apache Ant Latest Stable Version
http://ant.apache.org
Step 2: Extract the Zip file
Step 3: Go to Environment Variables
Depends is a very useful command which will execute the depends target before executing the current target. It is like parent child relationship.
For example, let say there is Java Class file HelloWorld.java you need to compile, jar, move and run. In command you will perform the following activities
To Compile
javac HelloWorld.java
To Create the jar file
jar -cvf HelloWorld.jar HelloWorld.class
or
jar -cvf HelloWorld.jar HelloWorld.class jar -cvf HelloWorld.jar HelloWorld.java
To Move the program
move HelloWorld.jar HelloWorld1.jar
To run the program
java -cp HelloWorld.jar HelloWorld
Executing all the above steps is ok for one time, lets say you have to do 100 times in a month. This is troublesome and can be automated.
Lets see how this can be automated
Step 1: Download the Apache Ant Latest Stable Version
http://ant.apache.org
Step 2: Extract the Zip file
Step 3: Go to Environment Variables
Step 4: Create variable ANT_HOME
Step 5: Add ANT_HOME\bin in the path
Step 6: Open Command prompt and execute the command ant -version
Step 7: Create the build.xml
1. target element with name attribute
<target name="clean">
Create target for each of your activities example, clean, compile, move, jar, execute etc.,
2. Echo Command
<echo>Cleaning the directories</echo>
The echo command is useful while running the ant, it show which step it is currently in. Basically it is just print command.
3. Depends Attribute
<target name="init" depends="clean">
4. Console Commands
<javac srcdir="${currentfolder}\src" destdir="${currentfolder}\classes">
<classpath refid="classpath"/>
</javac>
For most of the generic commands you have Ant Task defined, tasks such as Javac, Jar, War etc.,
5. Project Root Element and Attributes
<project name="HelloWorld" default="execute" basedir=".">
The project element defines the project name the default target it should execute and the base directory from where it should run.
Sample build.xml file as below,
<project name="HelloWorld" default="execute" basedir=".">
<!-- <property file="build.properties"/> -->
<!-- Tasks to be done 1. clean 2. init 3. compile 4. jar 5. move 6.execute -->
<property name="currentfolder" location="C:\Temp"/>
<path id="classpath">
<!--<fileset dir="${lib.dir}" includes="*.jar"/>-->
</path>
<target name="clean">
<echo>Cleaning the ${build.dir} and ${dist.dir}</echo>
<delete dir="${currentfolder}\classes"/>
<delete dir="${currentfolder}\lib"/>
</target>
<target name="init" depends="clean">
<echo>Creating the required directories</echo>
<mkdir dir="${currentfolder}\classes"/>
<mkdir dir="${currentfolder}\lib"/>
</target>
<target name="compile" depends="init">
<echo>Compile the source files</echo>
<javac srcdir="${currentfolder}\src" destdir="${currentfolder}\classes">
<classpath refid="classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<echo>Creating the jar file</echo>
<jar jarfile="${currentfolder}\lib\HelloWorld.jar" basedir="${currentfolder}\classes"/>
</target>
<target name="move" depends="jar">
<echo>Moving the HelloWorld.jar file</echo>
<move file="${currentfolder}\lib\HelloWorld.jar" tofile="${currentfolder}\lib\HelloWorld1.jar"/>
</target>
<target name="execute" depends="move">
<echo>Executing Hello World</echo>
<java fork="true"
failonerror="true"
maxmemory="128m"
classname="HelloWorld">
<arg value="-h"/>
<classpath>
<pathelement location="${currentfolder}\lib\HelloWorld1.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
</project>
Step 8: Go to Command and run ant where you have the build.xml
This is just tip of an Ice Berg, you can perform lot of activities like deploying code to the server, creating war files, create ear files, creating stubs, skeletons, ftp etc., As long you can perform some operation in command line you can perform the same using ant but in a sequence.