62

I have a tomcat server working, and there I have a webapp folder my_web_app.

I didn't deploy the project; I only have that folder of that application (as TOMCAT_DIR/webapps/my_web_app).

What I need is a WAR file. How can I create a .war file from this webapp?

4 Answers 4

143

You can create .war file back from your existing folder.

Using this command

cd /to/your/folder/location
jar -cvf my_web_app.war *
3
  • What about the libraries that the project uses that are in Tomcar's lib folder? Do they need to be put inside the war file? Will that war file work in another servlet container like JBoss? Jul 21, 2015 at 16:15
  • @ketan Does the command require any input files or manifest because I got an error (saying that the command requires the input or manifest files) when I followed your steps? Could you help me? Also FYI, I browsed for the error I got but it (stackoverflow.com/questions/23422470/…) doesn't seem to help.
    – Avinesh
    Mar 12, 2016 at 12:40
  • I used this to create a war file from the results of webpack, and tomcat liked it.
    – jkerak
    Sep 19, 2016 at 20:45
25

Its just like creating a WAR file of your project, you can do it in several ways (from Eclipse, command line, maven).

If you want to do from command line, the command is

jar -cvf my_web_app.war * 

Which means, "compress everything in this directory into a file named my_web_app.war" (c=create, v=verbose, f=file)

1
  • One like for the explanation of the 3 flags (I haven't seen this anywhere else.) Jul 6, 2017 at 11:57
5

There is a way to create war file of your project from eclipse.

First a create an xml file with the following code,

Replace HistoryCheck with your project name.

<?xml version="1.0" encoding="UTF-8"?>
<project name="HistoryCheck" basedir="." default="default">
    <target name="default" depends="buildwar,deploy"></target>
    <target name="buildwar">
        <war basedir="war" destfile="HistoryCheck.war" webxml="war/WEB-INF/web.xml">
            <exclude name="WEB-INF/**" />
            <webinf dir="war/WEB-INF/">
                <include name="**/*.jar" />
            </webinf>
        </war>
    </target>
    <target name="deploy">
        <copy file="HistoryCheck.war" todir="." />
    </target>
</project>

Now, In project explorer right click on that xml file and Run as-> ant build

You can see the war file of your project in your project folder.

1
  • Getting this error... Target buildwar does not exists in the project"<proj-name>. It is used from the target default.
    – Jay Godara
    Dec 24, 2014 at 11:09
2

Create the war file in a different directory to where the content is otherwise the jar command might try to zip up the file it is creating.

#!/bin/bash

set -euo pipefail

war=app.war
src=contents

# Clean last war build
if [ -e ${war} ]; then
    echo "Removing old war ${war}"
    rm -rf ${war}
fi

# Build war
if [ -d ${src} ]; then
    echo "Found source at ${src}"
    cd ${src}
    jar -cvf ../${war} *
    cd ..
fi

# Show war details
ls -la ${war}

Not the answer you're looking for? Browse other questions tagged or ask your own question.