Accepted answer

you can use ant to set up, compile, war, and deploy your solution.

<target name="default" depends="setup,compile,buildwar,deploy"></target>

you can then execute one click in eclipse to run that ant target. here are examples of each of the steps:


we'll assume that you have your code organized like:

  • ${basedir}/src: java files, properties, xml config files
  • ${basedir}/web: your jsp files
  • ${basedir}/web/lib: any jars required at runtime
  • ${basedir}/web/meta-inf: your manifest
  • ${basedir}/web/web-inf: your web.xml files

set up

define a setup task that creates the distribution directory and copies any artifacts that need to be warred directly:

<target name="setup">
    <mkdir dir="dist" />
    <echo>copying web into dist</echo>
    <copydir dest="dist/web" src="web" />
    <copydir dest="dist/web/web-inf/lib" src="${basedir}/../web/web-inf/lib" />


build your java files into classes and copy over any non-java artifacts that reside under src but need to be available at runtime (e.g. properties, xml files, etc.):

<target name="compile">
    <delete dir="${dist.dir}/web/web-inf/classes" />
    <mkdir dir="${dist.dir}/web/web-inf/classes" />
    <javac destdir="${dist.dir}/web/web-inf/classes" srcdir="src">
            <fileset dir="${basedir}/../web/web-inf/lib">
                  <include name="*" />
    <copy todir="${dist.dir}/web/web-inf/classes">
        <fileset dir="src">
            <include name="**/*.properties" />
            <include name="**/*.xml" />

build war

create the war itself:

<target name="buildwar">
    <war basedir="${basedir}/dist/web" destfile="my.war"
        <exclude name="web-inf/**" />
        <webinf dir="${basedir}/dist/web/web-inf/">
            <include name="**/*.jar" />


finally, you can set up a task to deploy the war directly into your tomcat deploy location:

<target name="deploy">
    <copy file="my.war" todir="${tomcat.deploydir}" />

click and go!

once all this is set up, simply launching the default target from eclipse will compile, war, and deploy your solution.

the advantage of this approach is that it will work outside eclipse as well as within eclipse and can be used to easily share your deployment strategy (e.g. via source control) with other developers who are also working on your project.


**making war file in eclips gaynemed of grails web project **

1.import project:

2.change the datasource.groovy file

like this: url="jdbc:postgresql://"

2.chnge appconfig.xml

3.kill the java from task manager:

  1. run clean comand in eclips

  2. run 'prod war' fowllowed by project name.

  3. check the log file and find the same .war file in directory of workbench with same date.


simpler solution which also refreshes the eclipse workspace:

<?xml version="1.0" encoding="utf-8"?>
<project name="project" default="default">    
    <target name="default">
        <war destfile="target/myapplication.war" webxml="web/web-inf/web.xml">
            <fileset dir="src/main/java" />
            <fileset dir="web/web-inf/views" />
            <lib dir="web/web-inf/lib"/>
            <classes dir="target/classes" />
        <eclipse.refreshlocal resource="myapplication/target" depth="infinite"/>


simplistic shell code for creating war files from a standard eclipse dynamic web project. uses ram file system (/dev/shm) on a linux platform.


utility=$(basename $0)

if [ -z "$1" ] ; then
    echo "usage: $utility [-s] <web-app-directory>..."
    echo "       -s ..... with source"
    exit 1

if [ "$1" == "-s" ] ; then

while [ ! -z "$1" ] ; do


    if [ ! -d $web_app_dir ] ; then
        echo "\"$web_app_dir\" is not a directory"

    if [ ! -d $web_app_dir/webcontent ] ; then
        echo "\"$web_app_dir\" is not a web application directory"


    mkdir $tmp_dir

    pushd $web_app_dir > /dev/null
    cp -r webcontent/* $tmp_dir
    cp -r build/* $tmp_dir/web-inf
    [ ! -z "$with_source" ] && cp -r src/* $tmp_dir/web-inf/classes
    cd $tmp_dir > /dev/null
    [ -e $war_file ] && rm -f $war_file
    jar cf $war_file .
    ls -lsf $war_file
    popd > /dev/null

    rm -rf $tmp_dir


another option would be to build it automatically using eclipse. of course if you have continuous integration environment ant or maven is recommended. the export alternative is not very convenient because you have to configure every time the export properties.


  1. enable "project archives" support; this might depend on your project (i used it on java ee/web project). right-click project root directory; configure -> add project archives support.

  2. go and create a new archive in the "project archives" top dir. you have only jar option, but name you archive *.war.

  3. configure fileset-s, i.e what files to be included. typical is to configure two filesets similar how the web deployment assembly (project property) is configured.

    • copy /webcontent to /
    • copy /build/classes to web-inf/classes (create this fileset after you define the web-inf/classes directory in the archive)
  4. you might need to tweek the fileset exclude property depending where you placed some of the config files or you might need more filesets, but the idea is that once you configured this you don't need to change it.

  5. build the archive manually or publish directly to server; but is also automatically built for you by eclipse


another common option is gradle.

to build your war file in a web app:

in build.gradle, add:

apply plugin: 'war'


./gradlew war

use the layout from accepted answer above.


use ant build code i use this for my project sms

<property name="web-inf" value="${basedir}/webroot/web-inf" />
<property name="out" value="${basedir}/out" />
<property name="war_file_name" value="mywebapplication.war" />
<property name="temp" value="${basedir}/temp" />

<target name="help">
        compile - compile
        archive - generate war file

<target name="init">
    <delete dir="${web-inf}/classes" />
    <mkdir dir="${web-inf}/classes" />

<target name="compile" depends="init">
    <javac srcdir="${basedir}/src" 

<target name="archive" depends="compile">
    <delete dir="${out}" />
    <mkdir dir="${out}" />
    <delete dir="${temp}" />
    <mkdir dir="${temp}" />
    <copy todir="${temp}" >
        <fileset dir="${basedir}/webroot">
    <move file="${temp}/" 
                    todir="${temp}/web-inf/classes" />
    <war destfile="${out}/${war_file_name}" 
                    webxml="${temp}/web-inf/web.xml" />
    <delete dir="${temp}" />

<path id="libs">
    <fileset includes="*.jar" dir="${web-inf}/lib" />


use the ant war task


use the following command outside the web-inf folder. this should create your war file and is the quickest method i know.

(you will need jdk 1.7+ installed and environment variables that point to the bin directory of your jdk.)

jar -cvf projectname.war *

reference link


if you are not sure what to do and are starting from scratch then maven can help get you started.

by following the the below steps you can get a new war project setup perfectly in eclipse.

  1. download and install maven
  2. go the command line run: mvn archetype:generate
  3. follow the prompted steps - choosing the simple java web project (18) and a suitable name.
  4. when it is finished run: mvn eclipse:eclipse
  5. start eclipse. choose file -> import -> existing project. select the directory where you ran the mvn goals.
  6. that's it you should now have a very good start to a war project in eclipse
  7. you can create the war itself by running mvn package or deploy it by setting up a server in eclipse and simply adding adding the project to the server.

as some others have said the downside of using maven is that you have to use the maven conventions. but i think if you are just starting out, learning the conventions is a good idea before you start making your own. there's nothing to stop you changing/refactoring to your own preferred method at a later point.

hope this helps.


a war file is simply a jar file with a war extension, but what makes it work is how the contents is actually structured.

the j2ee/java ee tutorial can be a start:

and the servlet specification contains the gory details:

if you create a new web project in eclipse (i am referring to the java ee version), the structure is created for you and you can also tell it where your appserver is installed and it will deploy and start the application for you.

using the "export->war file" option will let you save the war file.


we use maven (ant's big brother) for all our java projects, and it has a very nifty war plugin. tutorials and usage can be found there.

it's a lot easier than ant, fully compatible with eclipse (use maven eclipse:eclipse to create eclipse projects) and easy to configure.

maven's homepage

maven war plugin

sample configuration:



i've always just selected export from eclipse. it builds the war file and includes all necessary files. providing you created the project as a web project that's all you'll need to do. eclipse makes it very simple to do.

Related Query

More Query from same tag