Java 如何解包和重新打包 WAR 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19269961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:35:27  来源:igfitidea点击:

How to unpackage and repackage a WAR file

javamavenweb-applicationsjboss7.xwar

提问by Vinoth Kumar C M

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

我有一个 WAR 文件。我想打开它,编辑一个 XML 文件,删除一些 jar,然后重新打包它。

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.

我使用 WINRAR 打开 WAR 文件并删除了一些 Jars 并在 WinRar 中执行了“添加到存档”并创建了一个 WAR。

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

当我在 jboss 文件夹中部署 WAR 时,出现异常。

   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 ?

如何重新打包 WAR ?

回答by Juned Ahsan

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

您可以使用此处提到的 java 命令从命令行更新您的战争:

jar -uvf test.war yourclassesdir 

Other useful commands:

其他有用的命令:

Command to unzip/explode the war file

解压/分解war文件的命令

jar -xvf test.war

Command to create the war file

创建war文件的命令

jar -cvf test.war yourclassesdir 

Eg:

例如:

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

回答by Whome

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.

我确信有 ANT 标签可以做到这一点,但在 .bat 脚本中使用了这个 7zip hack。我使用http://www.7-zip.org/命令行工具。我一直使用它来更改 j2ee context.xml 文件中的 jdbc url。

mkdir .\temp-install
c:\apps\commandsza.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\commandsza.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.

您可以提取整个 .war 文件(毕竟是 zip 文件)、删除文件、替换文件、添加文件、修改文件并重新打包为 .war 存档文件。但是更改大型 .war 存档中的一个文件,这可能最好提取特定文件,然后更新原始存档。

回答by Barun

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

也许,您已经修改了战争的结构或将其部署在不同的服务器版本上。查看这些链接Error deploying war into JBoss AS 7 (domain mode): “Failed to process phase STRUCTURE of deployment”https://community.jboss.org/thread/199387?start=0&tstart=0&_sscc=t

回答by Griknok

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

以非编程方式,您只需使用 7zip UI 打开存档即可添加/删除或提取/替换文件,而无需更改结构。直到现在我才知道使用其他东西有问题:)

回答by Griknok

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

改编自上述答案,这适用于 Tomcat,但也适用于 JBoss 或任何容器:

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

回答by yasin

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

不用说,tomcat 很自然的将war 文件解压到同名文件夹中。您只需修改该文件夹中所需的文件(包括 .xml 配置文件),仅此而已。技术上不需要在应用修改后重新启动 tomcat

回答by Barani r

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

将您的战争文件复制到 /tmp 现在提取内容:

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.

现在您在 /tmp/warfile.war 中更新了您的文件。

回答by Agam

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 ..