56

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 ?

Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130

8 Answers8

88

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
Lucky
  • 16,787
  • 19
  • 117
  • 151
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • what is this for ? Extracting or repackaging ? – Vinoth Kumar C M Oct 09 '13 at 11:05
  • 1
    @VinothKumar uvf option is to update your war file, cvf option is to create, xvf is to unzip or explode. – Juned Ahsan Oct 09 '13 at 11:08
  • 3
    it doesnt work, it doesnt keep the same file structure – Guy Lubovitch Apr 21 '14 at 02:25
  • 9
    I use the following commands and it works: `cd [path]/yourclassesdir jar -cvf ../test.war *` – Dan Sin Dec 11 '15 at 03:18
  • 2
    The `v` option is not required to do these actions. Its output may be annoying or useful depending on your preferences. From the `jar` man page: `v Generates verbose output to standard output.` I generally prefer not using verbose output, and think it is useful to distinguish unnecessary verbose flags from necessary `v` flags that mean things other than verbose. – Ryan Heathcote Mar 03 '16 at 16:41
  • 1
    Might be obvious for some, but still... Never run `cvf` on live web container managed (Tomcat etc) exploded web application. It will corrupt the WAR to be created and destroy current directory. Web container thinks it is a new WAR and removes your current files that are being archived. – Alex Pakka Mar 23 '17 at 14:55
14

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.

Barani r
  • 2,119
  • 1
  • 25
  • 24
6

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 ..
Agam
  • 1,015
  • 2
  • 11
  • 21
4

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
2

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.

Whome
  • 10,181
  • 6
  • 53
  • 65
1

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 :)

Griknok
  • 384
  • 2
  • 13
  • 1
    Not possible since it will show Operation not supported – Lucky Apr 15 '20 at 19:02
  • Do you mean "Unsupported command"? You have not selected the correct executable in the 7zip installation directory. You need to use 7zFM.exe (the 7zip UI executable). WAR files and JAR files use the *exact* same format as zip https://superuser.com/questions/274229/whats-the-difference-between-the-war-and-zip-file-formats – Griknok Apr 22 '20 at 23:16
0

Maybe, you have modified the structure of the war or deploying it on a different server version. Checkout these links Error deploying war into JBoss AS 7 (domain mode): "Failed to process phase STRUCTURE of deployment" and https://community.jboss.org/thread/199387?start=0&tstart=0&_sscc=t

Community
  • 1
  • 1
Barun
  • 1,520
  • 2
  • 12
  • 18
-1

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

yasin
  • 270
  • 4
  • 6