concept Java program in category ant

This is an excerpt from Manning's book Ant in Action.
Ant is Java-based and tries to hide all the platform details it can. It’s also highly extensible in Java itself. This makes it easy to extend Ant through Java code, using all the functionality of the Java platform and third-party libraries. It also makes the build very fast, as you can run Java programs from inside the same Java virtual machine as Ant itself.
Listing 2.1. Our first complete build file, including packaging and executing a Java program
<?xml version="1.0" ?> <project name="secondbuild" default="execute" > <description>Compiles and runs a simple program</description> <target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist" /> </target> <target name="compile" depends="init" description="Compiles the source code"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="archive" depends="compile" description="Creates the JAR file"> <jar destfile="dist/project.jar" basedir="build/classes" /> </target> <target name="clean" depends="init" description="Removes the temporary directories used"> <delete dir="build" /> <delete dir="dist" /> </target> <target name="execute" depends="compile" description="Runs the program"> <echo level="warning" message="running" /> <java classname="org.antbook.welcome.Main" classpath="build/classes"> <arg value="a"/> <arg value="b"/> <arg file="."/> </java> </target> </project>
The other option—the easy option—is using Ant’s <java> or <exec> task to run the program straight from a build file. Ant can run Java and native programs with ease. Java programs can even run inside Ant’s own JVM for higher performance. Figure 6.1 illustrates the basic conceptual model of this execution. Many Ant tasks work by calling native programs or Java programs. As a case in point, the <javac> task can run Sun’s javac compiler in Ant’s JVM, or IBM’s Jikes compiler as a native application.
Java programs are easy to work with, as the classpath specification and JVM options make controlling the execution straightforward. In-JVM execution has a faster startup, but external execution is more trouble-free, which makes it a good choice.
For Java programs to be callable from Ant, they should be well documented. Ideally, they should have a library API as well as a public entry point. The API enables Java programs to use the external program as a set of classes, rather than just as something to run once. This makes migration to a custom task much easier. The programs should let you set the base directory for reading in relative information, or have parameters setting the full paths of any input and output files used.
When calling a Java program, we recommend that you