Search

Sample Ant Build File - JAR

In this example you will see how to structure the project. If the grows bigger, it will become a problem to manages the files if all the files are there in the same directory.
For a easier maintenanace all the source file should be kept in the src directory, the compressed jar file should be kept in the dist directory and all the intermediate class files should be kept in thebuild/classes directory.
By imposing structure cleaning the project becomes easy, we can just delete the directory and recreate it.
Using the <mkdir> task we create build/classes and dist directory.

1.<target name="init">
2.<mkdir dir="build/classes" />
3.<mkdir dir="dist" />
4.</target>
During the compilation process all the java files in the src directory will be compiled and the generated class files will be placed in the build/classes directory.
Since we placed all the class files under the build/classes directory, creating jar file becomes easier, you can simply specify the basedir attribute as "build/classes" instead of specifying basedir="." andincludes="*.class". After creating the jar file we place it in the dist directory (destfile="dist/HelloWorld.jar").

1.<target name="compile" depends="init">
2.<javac srcdir="src" destdir="build/classes" />
3.</target>
4. 
5.<target name="compress" depends="compile">
6.<jar destfile="dist/HelloWorld.jar" basedir="build/classes" />
7.</target>
You can use the java task to execute a class file as shown below. The classname attribute refers to the java class to be executed and the classpath attribute refers to the directory in which the class is located.

1.<target name="execute" depends="compile">
2.<java classname="com.vaannila.helloworld.HelloWorld"classpath="build/classes" />
3.</target>
Since all the class files and jar files are isolated, we can easily clean the project by deleting the respective directories.

1.<target name="clean">
2.<delete dir="build" />
3.<delete dir="dist" />
4.</target>
The default target is compress, so when you run the build file the compress target will be executed. The compress target depends on compile target which in turn depends on the init target, so first theinit target will be executed and the two directories will be created, then the compile target will be execute, later the jar file is created.
 When you run the "ant execute" command the execute target will be invoked. Execute target also depends on compile target which in turn depends on init target, but now the directories won't be created again because it already exist. Since the java file is already compiled it won't be compiled again. Ant checks the timestamp of the file and compiles only the updated files. The HelloWorld.classfile will be execute and the "HelloWorld!" message will be displayed on the console.

To clean and execute the program run the "ant clean execute" command. First the clean target will be executed and the directories will be deleted, later the execute task will be invoked.

No comments:

Post a Comment