Java ANT Projects

Java ANT Project 1

Java ANT Examples


How you can create a jar file with the using of Ant

Previous Home Next

The Ant specially using to create the jar file , document files war files , so this topic we are creating a jar file with the using of Ant.

To ensure that you install the Ant. Then unzip the downloading file and creating the build.xml file.

The execution of ANT file is done from the directory where build.xml resides and simply typing ant command on console and hit enter will start the execution of build file.

<?xml version="1.0" ?> 
<project name="java" default="compress">
	<target name="compile">
        <javac srcdir="com/java/log4j"/>
        <echo> Compile the files Complete! </echo>
    <target name="compress" depends="compile">
      <jar destfile="java.jar" basedir="." includes="*.class"/>
           <echo> Building .jar file Complete! </echo>

In this build.xml file the <project name> is the root file element it means the name attribute is represented to the name of the project.

<target>element can exist multiple times within project element and each one of them represents a single stage.

In the above build.xml, we have compile target element which contains two tasks, one - <echo>and the other <javac> which builds java files. The other target element 'compress' is dependent upon compile target element.

It creates a jar file which includes all the .class files in current directory.

Then creating a jar file using ant.

The most of the time ant is using to compile and build the java projects. These means it is generating the file and then compiling and building the jar files. Firstly we created the build.xml file in the directory where sources are located. The sources are contain the subdirectory there name is src. we also need to including a jar file where compiling.

The ant task contain a few tasks for doing the following operations:

  1. when you create the jar file then firstly you clean the build directory if exists.
  2. create the build directory where the new classes will be built.
  3. Compile the java classes.
  4. Build the jar file from the java classes.

<project name="usingant" basedir="." default="jar">
<property name="build" value="build"/>
	<property file=""/>
	<target name="clean">
		<delete dir="${build}"/>
<target name="init" depends="clean">
		<mkdir dir="${build}"/>
 <target name="compile" depends="init">
 <!-- Compile the java code -->
	<javac srcdir="src" destdir="${build}">
	   <pathelement location="${env.LIB}/required-jar1.jar"/>
		 <pathelement location="${env.LIB}/required-jar2.jar"/>
  <target name="jar" depends="compile">
	<!-- Build the jar file -->
	 <jar basedir="${build}" destfile="${build}/new-jar.jar"/>

Previous Home Next