Tuesday, April 30, 2013

How to create war file using ANT(Build Management Tool)?

In this post we will see how to create WAR(Web ARchive ) file using Apache ANT. And we will also see how this build.xml flow is created !!
What is Apache ANT ?
Apache ANT
Apache ANT is software to perform automated build process .It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects.The most immediately noticeable difference between Ant and Make is that Ant uses XML to describe the build process and its dependencies, whereas Make uses Makefile format. By default the XML file is named build.xml.

What is the general flow of various targets involved in build.xml ?
Build.xml Target Flow
Steps involved:
1. At the execution of this build.xml the default target specified is get called first.
<project name="firstProject" default="All" basedir="."> 
Now controlled is transfer to the target named All(case sensitive).
2. Target All depends on target dist.
<target name="All" depends="dist" description="Clean work dirs, then compile and create a WAR"/> 
3. Target dist depends on target compile at this step. In order to create a bundled WAR file we need set of compiled clas files and web pages.
<target name="dist" depends="compile" description="Create WAR file for binary distribution"> 

<war destfile="${dist.home}/firstProject.war"> 

<fileset dir="${work.home}"/> 

<classes dir="${work.home}/WEB-INF/classes"/> 

</war> 

</target> 
4. Before proceeding with compilation part , we must create a directory structure to place this class files and web project related JSP files.
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir"> 

<mkdir dir="${dist.home}"/> <mkdir dir="${work.home}/WEB-INF/classes"/>

<!-- Copy static HTML and JSP files to work dir –> 

<copy todir="${work.home}"> 

<fileset dir="${web.home}"/> 

</copy> 

</target> 
5. To make sure that all old files and folders are cleared, just proceed with clean up activity before preparation.
<target name="clean" description="Delete old work and dist directories">

<delete dir="${work.home}"/> 

<delete dir="${dist.home}"/> 

</target> 
Complete Build.xml used to create WAR:
<?xml version="1.0" encoding="UTF-8"?>

<project name="firstProject" default="All" basedir=".">  
  <!-- Define the properties used by the build -->
  <property name="app.name"     value="Nielsen_Portal_New1"/>
  <property name="tomcat.home"     value="${basedir}/lib/tomcat-lib" />
  <property name="struts.home"     value="${basedir}/lib/struts-lib" />
  <property name="hibernate.home"  value="${basedir}/lib/hibernate-lib" />
  <property name="axis2.home"      value="${basedir}/lib/axis-lib" />
  <property name="poi.home"        value="${basedir}/lib/pom-lib" />
  <property name="mysql.home"      value="${basedir}/lib/mysql-lib" />
  <property name="log4j.home"      value="${basedir}/lib/log4j-lib" />
  <property name="work.home"       value="${basedir}/work"/>
  <property name="dist.home"       value="${basedir}/dist"/>
  <property name="src.home"        value="${basedir}/src"/>
  <property name="web.home"        value="${basedir}/WebContent"/>
  
   <!-- Define the CLASSPATH -->
  <path id="compile.classpath"> 
    <fileset dir="${tomcat.home}">
      <include name="*.jar"/>
    </fileset>
 <fileset dir="${struts.home}">
      <include name="*.jar"/>
    </fileset>
 <fileset dir="${hibernate.home}">
      <include name="*.jar"/>
    </fileset>
 <fileset dir="${axis2.home}">
      <include name="*.jar"/>
    </fileset>
 <fileset dir="${poi.home}">
      <include name="*.jar"/>
    </fileset>
 <fileset dir="${log4j.home}">
      <include name="*.jar"/>
    </fileset>
 <fileset dir="${mysql.home}">
      <include name="*.jar"/>
    </fileset>
  </path>
  
  <target name="All" depends="dist" description="Clean work dirs, then compile and create a WAR"/>
  
  <target name="clean" description="Delete old work and dist directories">
    <delete dir="${work.home}"/>
    <delete dir="${dist.home}"/>
  </target>

  <target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
    <mkdir  dir="${dist.home}"/>
    <mkdir  dir="${work.home}/WEB-INF/classes"/>
    <!-- Copy static HTML and JSP files to work dir -->
    <copy todir="${work.home}">
      <fileset dir="${web.home}"/>
    </copy>
  </target>

  <target name="compile" depends="prepare"
          description="Compile Java sources and copy to WEB-INF/classes dir">
    <javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes" debug="true" debuglevel="lines,vars,source">
 <classpath refid="compile.classpath"/>
    </javac>     <copy  todir="${work.home}/WEB-INF/classes">
      <fileset dir="${src.home}" excludes="**/*.java"/>
    </copy>
  </target>


  <target name="dist" depends="compile"
          description="Create WAR file for binary distribution">
    <war destfile="${dist.home}/firstProject.war">
   <fileset dir="${work.home}"/>
   <classes dir="${work.home}/WEB-INF/classes"/>
    </war>
  </target>
  </project> 
That’s it !! Its’ very simple, in the forthcoming posts we will see how to create a bundled EAR in Archived form and Exploded form. Leave a reply if you have any doubts regarding this post !!
References:
1] http://en.wikipedia.org/wiki/Apache_Ant
2] http://en.wikipedia.org/wiki/List_of_build_automation_software

No comments:

Post a Comment