Creating a deployable WAR file from Eclipse Project

Recently I was posed with a challenge of coming up with a solution to a seemingly simple problem “I want to use Eclipse to create by web mapping project and then deploy the solution directly into my Tomcat instance”

Sounds simple right, I started exploring ways to coming up with a solution. Eclipse allows us to run the application and then export the application to a standard WAR file. But then someone has to manually do the following steps :

  • Export the project to a WAR file using Eclipse Interface
  • Copy the WAR file from the location created to the Tomcat webapps folder

Here is a better alternative and a solution to the question :

Create an ANT script that will look at the Eclipse Project structure and build the WAR file and copy over the WAR file to the destination (Tomcat WebApps folder)

Here is the ANT script:

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Deploy From Eclipse to Tomcat" basedir=".">
<property name="warfile" value="sanfran"/>
<target name="unpack">
<unwar src="${warfile}.war" dest="${warfile}" />
<target name="create">
<war destfile="${warfile}.war" webxml="WebContent/WEB-INF/web.xml" update="true">
<classes dir="build\classes"/>
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml"/>
<target name="copy">
<copy todir="c:\tomcat5517\webapps" overwrite="true">
<fileset dir=".">
<include name="*.war"/>
<target name="deploy">
<antcall target="create"/>
<antcall target="copy"/>

Save the above code into a file and name it “build.xml”. Place the build.xml into the root folder of your eclipse project. For example, if “c:\projects” is my eclipse workspace and “SanFrancisco” is the name of my project then all the files related to the project will be under “c:\projects\SanFrancisco”.

You can run the ant command inside the projects root folder either using any ANT tool installed or if you have ArcGIS Java ADF installed then there is a customized ANT tool executable available for use.

Here is the usage:

1. Create the WAR file and deploy it to tomcat

"c:\program files\arcgis\java\tools\ant\build\arcgisant" deploy

2. Just create the WAR file

"c:\program files\arcgis\java\tools\ant\build\arcgisant" create

3. Just copy over the WAR file to deploy location

"c:\program files\arcgis\java\tools\ant\build\arcgisant" copy

4. Unpack the WAR file to see the contents

"c:\program files\arcgis\java\tools\ant\build\arcgisant" unpack


59 Responses to “Creating a deployable WAR file from Eclipse Project”

  1. Elph Says:

    Uh, nice, just what I need… Thanks

  2. Dave Says:

    Perfect! Thanks for this.

  3. krishnaroopa Says:

    Thanks. I used it to deploy to JBOSS

  4. Vivek Says:

    Thanks…that’s what i need

  5. ghostinthepuppet Says:

    Thank you very much for sharing!

  6. Suresh Says:

    Thanks it was really useful for me

  7. js Says:

    Nice! It was very helpful. Thank You!

  8. boutfounast Says:

    thanks so much friend ^^

  9. Moissane Says:

    I was stuck with a war deploy, and this solve my problem…thank you!

  10. en Says:

    Thank you

  11. Mukesh Says:

    man! luv dis script…ws luking for dis for so long. Its workin grt :)

  12. Lucas Says:

    Thank you dude, it really helped me at the right moment!

  13. Thomas Says:

    I hate ant, but I love simple solutions. Many thanks!

  14. devill Says:

    Can you please tell me how to deploy a web application(WAR) in tomcat using ant with eclipse ide?

  15. devill Says:

    How can we deploy a web application(WAR file) in tomcat , using ant with ,eclipse IDE?

  16. sms Says:

    Thank you dude, it really helped me at the right moment! for deploying my application

    Thanks a lot

  17. Daulat Says:

    Hey thanks for this man

  18. Anoop K Says:

    Thank you soo much.I was looking for the same!

  19. Bill Says:

    Great post, worked like a charm

  20. Ray Says:

    I have to agree with everyone else this is a killer ant file. As simple as can be and works PERFECTLY. Thanks

  21. joshua Says:

    Hi, good documentation.
    I develop a web application using tomcat 5, spring mvc 2.5, eclipse 3.3.
    I have difficulty to deploy jar file from my local machine to remote tomcat server. I set up all info well. But when i deploy in build.xml on my local machine, this error

    /var/lib/tomcat5/webapps/vm_admin/build.xml:90: No route to host

    could you help me out?

  22. Patrick Says:

    Awesome. Always love an automated solution, and this worked great for me.

  23. Shaz Says:

    Cool!! :-) It wrked.

    Thanks for sharing

  24. webmaster Says:

    I have to agree with everyone else this is a killer ant file
    Thank you soo much.I was looking for the same!

  25. webmaster1 Says:

    Great post
    Awesome. Always love an automated solution, and this worked great for me.

  26. booster Says:

    I hate ant, but I love simple solutions


  27. aadi Says:

    How can we deploy a web application(WAR file) in tomcat , using ant with ,eclipse IDE?

  28. dexpress Says:

    Thank you dude it really works fine


  29. Titom Says:

    > How can we deploy a web application(WAR file) in tomcat , using ant with ,eclipse IDE?

    1/ Create a build.xml file that contains the stuff of the blog post
    2/ in Eclipse, go to Windows/Show View…/
    3/ in the Window, Select Ant/Ant
    4/ in the Package explorer or Navigator, drag the build.xml file to the Ant view
    5/ Be sure “Hide internal targets” is not checked (all the targets from the script are internal since no descriptions are available)
    6/ Target “copy” here deploys the WAR in the Tomcat webapps folder; if Tomcat is already running, the WAR will be expanded and available at once, if it is not running, just start it.

  30. oak tree Says:

    it really helped me

  31. pa Says:

    that’s what i need

  32. clean Says:

    Thanks for this one

  33. ele Says:

    Thank you dude it really works fine

  34. web Says:

    Thank you bud, It is simple and it works!

    here is some minor modification to include reference jars into the war package (root WEB-INF/lib/)


  35. web Says:

    <?xml version=”1.0″ encoding=”ISO-8859-1″?>
    <project name=”Deploy From Eclipse to Tomcat” basedir=”.”>
    <property name=”warfile” value=”sanfran”/>

    <fileset id=”project.classes” dir=”lib”>
    <include name=”javax.servlet-5.1.12.jar” />
    <include name=”ojdbc5.jar” />
    <include name=”sqljdbc4.jar” />

    <target name=”unpack”>
    <unwar src=”${warfile}.war” dest=”${warfile}” />
    <target name=”create”>
    <war destfile=”${warfile}.war” webxml=”WebContent/WEB-INF/web.xml” update=”true”>
    <classes dir=”build\classes”/>
    <lib refid=”project.classes” />
    <fileset dir=”WebContent”>
    <exclude name=”WEB-INF/web.xml”/>
    <target name=”copy”>
    <copy todir=”c:\tomcat5517\webapps” overwrite=”true”>
    <fileset dir=”.”>
    <include name=”*.war”/>
    <target name=”deploy”>
    <antcall target=”create”/>
    <antcall target=”copy”/>

  36. Anonymous Says:

    I cant understand this how to create it in my project.please help me.

  37. Anonymous Says:

    in wat name i should save this xml file

    • Anonymous Says:

      i have created one project in eclipse .i have to deploy a war file in to my project to run my jsp pages,the informaton given above its not sufficient can u guide me step by step.

  38. Anonymous Says:

    where should i add this files

  39. massimo Says:

    thx a lot
    works immediately

  40. barkim Says:

    Hey, thanks a lot. This solution give more free time to enjoy with many other problems and not with the fkn ant


  41. aletermi Says:

    Thanks a lot for sharing this! a very helpful working sample.

  42. CVP Says:

    Perfect document, perfect build file. Its actually getting for me to deploy on tomcat externally. This document helped alot.


  43. Kumaresan Says:

    That’s fantastic tool man. When u execute using ant, go to the root folder and then type >ant create.

    Once again thanks a lot.

  44. jaroos Says:

    Yeah! this is a really awesome tool. Here’s a step-by-step method:

    1. Rt. click your project name
    2. Select New > File (call it Build.xml)
    3. Copy and paste above text (xml posted by sathyaprasad)
    4. Edit following lines to reflect your setup:
    b. Sometimes, adding full paths to “web.xml”, “todir” etc. helps
    5. Right click “build.xml” > Run As > Ant Build
    6. You should see something like the following if successful.
    Buildfile: C:\workspace\MyWar\build.xml
    [unwar] Expanding: C:\workspace\MyWar\MyWar.war into C:\Tomcat 4.1\webapps\MyWar
    Total time: 1 second

    I removed the step to “copy” because I unpacked directly into the web folder. Hope this helps!

  45. jaroos Says:

    Not sure why 4(a) got erased…following was the line to edit:

  46. jaroos Says:

    Looks like the webpage is trying to parse the line…here it is again…final try…

  47. Sristi Lohani Says:

    Lot of thanks,its simple and it works fine…..great:)

  48. soumi Says:

    Hi, this article is really very useful.
    But, how to make a war file in eclipse using bea weblogic server?

  49. joash Says:

    excellent many thanks

  50. shalu Says:

    It really helped me

  51. Ep Says:

    Thanks, built my .war successfully;

  52. Haus Fay Hotel Says:

    Wonderful, what a website it is! This web site presents helpful facts to us, keep it up.

  53. PC Games Download Says:

    Woah! I’m really enjoying the template/theme of this website. It’s simple, yet effective.
    A lot of times it’s tough to get that “perfect balance” between user friendliness and appearance. I must say that you’ve done a amazing job with this.
    Additionally, the blog loads extremely quick for me on Safari.

    Excellent Blog!

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: