How to configure build.xml for Ant build tool

Introduction:
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications.
Today's post is about how to build java project using Ant build tool. This Demonstration has been performed on Eclipse IDE. But you can use whatever suits you.



Steps:
This step is to compile all the classes in the src directory and
place them in the build/classes directory. To do this first you need to add all
the lib files inside the "WebContent/WEB-INF/lib" directory to the classpath.
   <path id="compile.classpath" >
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name=".jar"/>
        <fileset>
   </path>  
    <!--*********************Init Target*******************************-->
All the classes inside the src directory should be compiled and placed in
a separate build/classes directory. The created war file will be placed inside
the dist directory. So first we create the build/classes and the dist directory.
The init target does this job.
   
<target name="init">
        <mkdir dir="build/classes"/>
        <mkdir dir="${war.folder}"/>
</target>
    <!--*********************Compile Target*******************************-->
The target compile uses the javac task to compile the java classes and it
depends on the target init, because only when you have the directory ready you
can place the classes inside them.The path we created earlier will be refered
here using the <classpath> element.    
 <target name="compile" depends="init">
        <javac destdir="build/classes" debug="true" srcdir="src">
            <classpath refid="compile.classpath"></classpath>
        </javac>
  </target>
    <!--*********************War Target*******************************-->
Now we can create the war file using the war task. To create the war file
 you need to specify the web directory, lib directory and the classes directory.
 The destfile attribute specifies the war file location and the webxml attribute
 specifies the web.xml file location.    
<target name="war" depends="compile">
        <war destfile="${war.folder}/dishayein.war" webxml="${web.dir}/WEB-INF/web.xml">
            <lib dir="${web.dir}/WEB-INF/lib"></lib>
            <classes dir="build/classes"></classes>
        </war>
</target>  
    <!--*********************Deploy Target*******************************-->
<target name="deploy" depends="compile">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </copy>
</target>
  <!--*********************Clean Target*******************************-->
use the clean target to clean the project. The clean target deletes the
build and the dist directory.
<target name="clean">
        <delete dir="${war.folder}"></delete>
        <delete dir="build"></delete>
</target>
   
Final build.xml file:
<?xml version="1.0" ?>
<project name="project-name" default="war">
 <property file="build.properties"/>

 <path id="compile.classpath" >
  <fileset dir="${web.dir}/WEB-INF/lib">
   <include name=".jar"/>
  </fileset>
 </path>
 
 <target name="init">
  <mkdir dir="build/classes"/>
  <mkdir dir="${war.folder}"/>
 </target>
 
 <target name="compile" depends="init">
  <javac destdir="build/classes" debug="true" srcdir="src">
   <classpath refid="compile.classpath"></classpath>
  </javac>
 </target>
 
 <target name="war" depends="compile">
  <war destfile="${war.folder}/dishayein.war" webxml="${web.dir}/WEB-INF/web.xml">
   <lib dir="${web.dir}/WEB-INF/lib"></lib>
   <classes dir="build/classes"></classes>
  </war>
 </target>
 
 <target name="deploy" depends="compile">
  <copy todir="${deploy.path}/${name}" preservelastmodified="true">
   <fileset dir="${web.dir}">
    <include name="**/*.*"/>
   </fileset>
  </copy>
 </target>
 <target name="clean">
  <delete dir="${war.folder}"></delete>
  <delete dir="build"></delete>
 </target>
 
</project>

Now, create build.properties. There is no need to create build.properties file but it has been recommended to create it. It is to define the properties that are going to be used in build.xml file.
name=      project-name
web.dir=     WebContent
tomcat.home=     tomcat-directory
appserver.home=    ${tomcat.home}/apache-tomcat-6.0.41

war.folder=     dist

deploy.path=    ${appserver.home}/webapps

tomcat.manager.url=   http://localhost:8080/manager
tomcat.manager.username= tomcat-username
tomcat.manager.password= tomcat-password

We are Done!!
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment