How to create war files

Go To


What are the best practices of creating war files (using eclipse) to run on tomcat? tutorials, links, examples are highly appreciated.

2009-06-16 14:06
by NoName


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.

2009-06-16 14:22
by David Citron
well I want to deploy it to tomcat. How should I change the targer - NoName 2009-06-18 17:27
Could you please elaborate? As long as ${tomcat.deploydir} points to your Tomcat context deployment directory, that should do it. No - David Citron 2009-06-18 22:27
Why is there " " in the buildwar targe - NoName 2011-07-27 11:52
Is there a Maven equivalent solution - Xonatron 2011-12-19 17:47
In the war target there is unnecessary ''. You have generated *.class files into this folder and excluding them does not work. Remove this exclude line from the war target it works - ankitjaininfo 2013-04-15 08:39
is the actually important for a webapp? i dont have to specify a main class as classes are called via the url mappings right - tObi 2014-06-17 10:43
Where do the Preconditions get set?

Is ${basedir} internal to Ant or does this need to be defined somewhere - thonnor 2015-01-29 13:21

This answer is needlessly complicated - Alex R 2015-03-28 17:41


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.

2009-06-16 14:12
by Mech Software
Thanks! and for what it's worth, you select "Export" from the "File" menu, then in "Select an Export Destination" type "WAR" and the war file option will come up - Brad Parks 2012-04-17 00:06
my choice for quick and dirty web apps. writing an ant script just to get a test WAR out is just too much work - Renan 2012-04-17 01:19
another solution instead of creating an ant file would to go to the folder you want to wrap in WAR file and execute this command "jar -cvf name.war *" - M.C. 2013-02-09 15:36
Thanks for this, a LOT simpler than configuring Maven or Ant (when it's not necesarily needed) - simon 2013-04-23 16:12


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:

2009-06-16 14:19
by mikek
I gave you your point back (though it wasn't I who removed it). Maven is certainly a reasonable choice if you happen to (want to?) fit into their particular version of "convention over configuration. - David Citron 2009-06-16 15:02
I happen to like it =) I'm working as CM on a pretty big project, and I appreciate having the grunt work of project configuration done for me. Also, the plugin support is wonderful. Being able to have a multi-module project up and running with a WAR-plugin, with Surefire and CheckStyle in five minutes is a treat.

Ant, while more open and arguably more powerful, is a hassle in comparison. Anyway, Maven is a subset to Ant, so if you need to do any scripting there, you're welcome - mikek 2009-06-16 15:08

Maven is Ant's big brother? They are not related at all.... : - Buhake Sindi 2012-06-28 09:29
Besides Maven's XML structure being based on ANT's, and ANT being incorporated as a subset of Maven - mikek 2012-06-29 08:51


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.

2009-06-16 14:23
by boxofrats


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.

2009-06-16 22:12
by Pablojim


Use the Ant war task

2009-06-16 14:17
by Ilya Boyandin


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" />

2009-11-12 05:56
by sms


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

2013-05-29 12:22
by arpadf


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.

2015-01-23 13:30
by Charlie Dalsass


Use this command outside the WEB-INF folder. This should create your war file. This is a quickest method I know.

You will need JDK 1.7+ installed to achieve this feat and environment variables that point to the bin directory of your JDK.

jar -cvf projectname.war *

Reference Link

2017-02-13 08:56
by XChikuX


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"/>
2015-03-28 17:40
by Alex R


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
2016-12-19 20:29
by Joe Horvath


**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.

2015-01-30 06:56
by Ratnesh Kumar