Simple Ant script
16 February 2008Let us see how to write a simple ant script for a Java application. Write a simple HelloWorld Java class
HelloWorld.Java
public class HelloWorld { public static void main(String args[]) { System.out.println("HelloWorld!"); } }
By default Ant uses build.xml as the name for a buildfile. This is the build.xml.
<project default="compile"> <target name="compile"> <javac srcdir = "." /> </target> <target name="jar" depends="compile"> <jar destfile = "HelloWorld.jar" basedir = "." includes = "**/*.class" /> </target> <target name = "run" depends = "jar"> <java classname = "HelloWorld" classpath = "HelloWorld.jar" fork = "true" /> </target> </project>
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.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Simple Ant script