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>
<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"/>
</fileset>
</war>
</target>
<target name="copy">
<copy todir="c:\tomcat5517\webapps" overwrite="true">
<fileset dir=".">
<include name="*.war"/>
</fileset>
</copy>
</target>
<target name="deploy">
<antcall target="create"/>
<antcall target="copy"/>
</target>
</project>

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

20 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 :)
    Thanks,
    Mukesh

  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
    SMS

    Thanks a lot

  17. Daulat Says:

    Hey thanks for this man


Leave a Reply