How to unpackage and repackage a WAR file

JavaMavenWeb Applicationsjboss7.xWar

Java Problem Overview


I have a WAR file. I would like to open it, edit an XML file, remove some jars and then re-package it.

I used WINRAR to open the WAR file and I removed some Jars and did an 'Add to Archive' in WinRar and created a WAR.

When I deployed the WAR in jboss folder, I got an exception.

   16:05:14,316 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) 
   MSC00001: Failed to start service jboss.deployment.unit."myapplication.war".
   STRUCTURE: org.jboss.msc.service.StartException in 
   service   jboss.deployment.unit."myapplication.war".STRUCTURE: 
   Failed to process phase STRUCTURE of deployment "myapplication.war"

How do I repackage the WAR ?

Java Solutions


Solution 1 - Java

you can update your war from the command line using java commands as mentioned here:

jar -uvf test.war yourclassesdir 

Other useful commands:

Command to unzip/explode the war file

jar -xvf test.war

Command to create the war file

jar -cvf test.war yourclassesdir 

Eg:

jar -cvf test.war *
jar -cvf test.war WEB-INF META-INF

Solution 2 - Java

copy your war file to /tmp now extract the contents:

cp warfile.war /tmp
cd /tmp
unzip warfile.war
cd WEB-INF
nano web.xml (or vim or any editor you want to use)
cd ..
zip -r -u warfile.war WEB-INF

now you have in /tmp/warfile.war your file updated.

Solution 3 - Java

This worked for me:

mv xyz.war ./tmp
cd tmp
jar -xvf xyz.war
rm -rf WEB-INF/lib/zookeeper-3.4.10.jar
rm -rf xyz.war
jar -cvf xyz.war *
mv xyz.war ../
cd ..

Solution 4 - Java

Adapting from the above answers, this works for Tomcat, but can be adapted for JBoss as well or any container:

sudo -u tomcat /opt/tomcat/bin/shutdown.sh
cd /opt/tomcat/webapps
sudo mkdir tmp; cd tmp
sudo jar -xvf ../myapp.war
#make edits...
sudo vi WEB-INF/classes/templates/fragments/header.html
sudo vi WEB-INF/classes/application.properties
#end of making edits
sudo jar -cvf myapp0.0.1.war *
sudo cp myapp0.0.1.war ..
cd ..
sudo chown tomcat:tomcat myapp0.0.1.war
sudo rm -rf tmp
sudo -u tomcat /opt/tomcat/bin/startup.sh

Solution 5 - Java

I am sure there is ANT tags to do it but have used this 7zip hack in .bat script. I use http://www.7-zip.org/ command line tool. All the times I use this for changing jdbc url within j2ee context.xml file.

mkdir .\temp-install
c:\apps\commands\7za.exe x -y mywebapp.war META-INF/context.xml -otemp-install\mywebapp
..here I have small tool to replace text in xml file..
c:\apps\commands\7za.exe u -y -tzip mywebapp.war ./temp-install/mywebapp/*
rmdir /Q /S .\temp-install

You could extract entire .war file (its zip after all), delete files, replace files, add files, modify files and repackage to .war archive file. But changing one file in a large .war archive this might be best extracting specific file and then update original archive.

Solution 6 - Java

Non programmatically, you can just open the archive using the 7zip UI to add/remove or extract/replace files without the structure changing. I didn't know it was a problem using other things until now :)

Solution 7 - Java

Solution 8 - Java

no need to that, tomcat naturally extract the war file into a folder of the same name. you simply modify the desired file inside that folder (including .xml configuration files), that's all. technically no need to restart tomcat after applying the modifications

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
QuestionVinoth Kumar C MView Question on Stackoverflow
Solution 1 - JavaJuned AhsanView Answer on Stackoverflow
Solution 2 - JavaBarani rView Answer on Stackoverflow
Solution 3 - JavaAgamView Answer on Stackoverflow
Solution 4 - Javauser2066936View Answer on Stackoverflow
Solution 5 - JavaWhomeView Answer on Stackoverflow
Solution 6 - JavaGriknokView Answer on Stackoverflow
Solution 7 - JavaBarunView Answer on Stackoverflow
Solution 8 - JavayasinView Answer on Stackoverflow