View RSS Feed

My Java Tips

Simple Ant script

Rate this Entry
by , 11-19-2011 at 05:49 PM (1935 Views)
Let us see how to write a simple ant script for a Java application. Write a simple HelloWorld Java class

Java Code:
public class HelloWorld
 	public static void main(String args[])
By default Ant uses build.xml as the name for a buildfile. This is the build.xml.
Java Code:
<project default="compile">

  	<target name="compile">
    		<javac srcdir=".">

	<target name="jar" depends="compile">
    		<jar destfile="HelloWorld.jar" basedir="." includes="**/*.class">

    	<target name="run" depends="jar">
    		<java classname="HelloWorld" classpath="HelloWorld.jar" fork="true">
A normal or regular build.xml file is a lot larger than the build.xml file above. However, to start with the above information is enough.

Now you can compile, package (jar) and execute (run) the application as follows:

1. Install ant
2. Change to the directory in the command prompt where you have this build.xml
3. Type ant compile at the command prompt. This will compile the java file
4. Type ant jar at the command prompt. This creates the jar file.
5. Type ant run at the command prompt. This executes the java file to print HelloWorld!
6. Type ant compile jar run at the command prompt. This will combine and shorten all the three steps above.

Submit "Simple Ant script" to Facebook Submit "Simple Ant script" to Digg Submit "Simple Ant script" to Submit "Simple Ant script" to StumbleUpon Submit "Simple Ant script" to Google