Meyyappan has posted 101 posts at DZone. View Full User Profile

Sample Ant Build File - JAR

06.13.2012
| 41924 views |
  • submit to reddit

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

 

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

John Jacobs replied on Sun, 2013/12/15 - 6:35pm

 I was supposed to do something like this for my Java class and I was confused about a few points. The tutorial here really helped me. Thanks so much.

Lisa Coffey replied on Fri, 2014/07/11 - 5:07am

 

Continuing our Hello World Fax Application project, let us add a new target to produce the jar files. But before that let us consider the jar task:

<jardestfile="${web.dir}/lib/util.jar"basedir="${build.dir}/classes"includes="faxapp/util/**"excludes="**/Test.class"/>

In this example, the web.dir property points to the path of the web source files. In our case, this is where the util.jar will be placed.

The build.dir property in this example points to the build folder where the class files for the util.jar can be found.

In this example, we create a jar file called util.jar using the classes from the faxapp.util.* package. However, we are excluding the classes that end with the name Test. The output jar file will be place in the webapp's online  folder.

If we want to make the util.jar an executable jar file we need to add the manifest with the Main-Class meta attribute.

Sumit Sachdeva replied on Tue, 2014/08/12 - 6:28am

Thanks ! for this article.

how to add external jar dependency, eg. log4j.jar?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.