Simple Ant script
by , 11-19-2011 at 04:49 PM (972 Views)
Let us see how to write a simple ant script for a Java application. Write a simple HelloWorld Java class
HelloWorld.Java
By default Ant uses build.xml as the name for a buildfile. This is the build.xml.Java Code:public class HelloWorld { public static void main(String args[]) { System.out.println("HelloWorld!"); } }
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.Java Code:<project default="compile"> <target name="compile"> <javac srcdir="."> </javac></target> <target name="jar" depends="compile"> <jar destfile="HelloWorld.jar" basedir="." includes="**/*.class"> </jar></target> <target name="run" depends="jar"> <java classname="HelloWorld" classpath="HelloWorld.jar" fork="true"> </java></target> </project>
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.









Email Blog Entry
Size Reduced for Images in PDF &...
05-15-2013, 05:53 PM in Java Software