Generate war file from tomcat webapp folder

JavaTomcat

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Using this command

cd /to/your/folder/location
jar -cvf my_web_app.war *

Solution 2 - Java

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)

Solution 3 - Java

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.

Solution 4 - Java

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}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneLRuLLView Question on Stackoverflow
Solution 1 - JavaKetanView Answer on Stackoverflow
Solution 2 - JavaCharu KhuranaView Answer on Stackoverflow
Solution 3 - JavanmkyuppieView Answer on Stackoverflow
Solution 4 - JavaGary DaviesView Answer on Stackoverflow