Creating and Compiling a war file in eclipse using ANT script

>>Here i am taking the example of Spring MVC web project. The structure of my project is:

  + – src
  |      +com.springexample.spring
  + – WebContents
        | – Index.jsp
        + – META-INF
            | –
        + – WEB_INF
            | – lib
                      (All jar files required for the project)
            | –  project-servlet.xml
            | –  web.xml
        + – jsps
            |- welcome_page.jsp
            |- goodbye_page.jsp

>>Next the structure of war file will be:


  |   Index.jsp
  + – META-INF
  + – WEB_INF
      |   project-servlet.xml
      |   web.xml
      + – classes
      |    (all class files generated during compilation)
      + – lib
          (All jar files required for the project)
 + – jsp
      |    (jsp i have used for mvc)


>>Now we are familiar with the structure of our project and the war file , we must follow the following steps to generate the war file:

  1. Firstly create a build.xml file in same project space, which will contain all the required instructions to run an ANT Script.
  2. Next we will create the projectlib folder in our project space , in which all the required jar files used to compile our project will be contained(we will packge these jars into our WAR file as well).
  3. In build.xml file we will define our project and few variables that point to src, webContent, build folder etc.
  4. Next we will define our class path.
    – In this section we will mention different locations for external jars that are required to compile our project.
    – like my projectlib folder which holds all the spring framework related jar files.
    – etc
  5. Now We will define some commands which will be automatically called from command line.
    – example
    ant clean
    ant build
  6.  The  ANT script will perform following steps
    – It will create “build” folder within the same project space, where all of the complied classes will be genrated
    – Now it will compile java files from src folder and keep their genreated class files in build/classes/ folder.
    – Now ANT script will create a war file by including contents of the webContent folder and remaining folders of ur project.
    – ANT script will also include all jar files from projectlib into SpringExample.war/WEB-INF/lib folder.
    – ANT script will also include all class files from build/classes into SpringWebExample.war/WEB-INF/classes folder.
  7. Finally the project is compiled and you can find the SpringExample.war file in the build folder.

>>ANT Script(build.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project name="SpringExample" basedir="." default="usage">
<property name="src.dir" value="src" />
<property name="webcontent.dir" value="WebContent" />
<property name="build.dir" value="${basedir}/build" />
<property name="name" value="SpringExample" />

<path id="project-classpath">

<!--  here give path for jar repository -->

<fileset dir="${basedir}/projectlib">
<include name="*.jar" />
<pathelement path="${build.dir}" />

<target name="build" depends="compile" description="build project, but first compile it">
<war destfile="build/${name}.war"
<lib dir="${basedir}/projectlib"/>
<classes dir="${build.dir}/classes" />
<fileset dir="${web.dir}">
<include name="**/*.*"/>

<target name="compile" description="Compile java files and place them webContents/web-inf/">
<mkdir dir="${build.dir}/classes"/>
<javac destdir="${build.dir}/classes" failonerror="true">
<src path="${src.dir}"/>
<classpath refid="project-classpath"/>

<target name="clean">
<delete dir="${build.dir}" failonerror="true"/>

>> Your war file is created.

Hope this helps you…🙂

Posted in JAVA. 1 Comment »

One Response to “Creating and Compiling a war file in eclipse using ANT script”

  1. word 2007 password recovery Says:

    Good day! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any tips?

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: