Sample Ant Build File - JAR
Join the DZone community and get the full member experience.Join For Free
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 the build/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.
<target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist" /> </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="." and includes="*.class" . after creating the jar file we place it in the dist directory ( destfile="dist/helloworld.jar" ).
<target name="compile" depends="init"> <javac srcdir="src" destdir="build/classes" /> </target> <target name="compress" depends="compile"> <jar destfile="dist/helloworld.jar" basedir="build/classes" /> </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.
<target name="execute" depends="compile"> <java classname="com.vaannila.helloworld.helloworld" classpath="build/classes" /> </target>
since all the class files and jar files are isolated, we can easily clean the project by deleting the respective directories.
<target name="clean"> <delete dir="build" /> <delete dir="dist" /> </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 the init 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.class file 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.
you can download the build file here.
build file : download
project : download
Opinions expressed by DZone contributors are their own.