如何使用命令行创建 WAR 文件? [英] How to create a WAR file using the commandline?
问题描述
我安装了 JBoss Developer Studio,可以通过鼠标右键项目 > 导出 > WAR 文件"创建 WAR 文件,但我想使用命令行将我的项目导出为 WAR 文件.
I installed JBoss Developer Studio and it's possible to create a WAR file by "right mouse project > Export > WAR file" but I want to export my project to a WAR file using the commandline.
我已经安装了 maven,这是 Studio 的要求之一,我读到我可以使用 maven 生成一个 WAR 文件,但我需要一个名为 pom.xml 的文件.当我搜索我的工作区和项目时,缺少 pom.xml.我可能需要手动创建 pom.xml,但我不确定如何.
I have maven installed which is one of the requirements of the Studio and I read that I can generate a WAR file using maven but I require a file called pom.xml. When I searched through my workspace and project, pom.xml was missing. I may need to manually create pom.xml but I'm unsure how to.
我的项目目录树如下:
Siesta
├── build
│ └── classes
├── src
└── WebContent
├── extjs
├── extjs-4.2.0
├── extjs-4.2.2
├── index.jsp
├── META-INF
├── siesta
├── tests
└── WEB-INF
如何使用命令行为我的 Maven/JBoss 项目创建 WAR 文件?我使用 Linux 并且不想创建 pom.xml 文件,但如果没有其他方法,那么我将使用 xml 文件来生成 war 文件.
How do I create a WAR file for my Maven / JBoss project using the command line? I use Linux and would prefer not having to create a pom.xml file but if there isn't another way, then I'll use the xml file to generate the war file.
所以jar是创建war文件的方法.我写了一个小脚本,它将为我创建一个特定目录的战争文件.
So jar is the way to go to create a war file. I wrote up a tiny script that will create a war file for me for a specific directory.
#!/bin/bash
cd Siesta/WebContent/
jar -cvf ../../Siesta.war *
cd -
然后,如果您在 ubuntu 中的 zip 实用程序或存档管理器中打开 war 文件,您将看到此结构
Then if you open the war file in a zip utility or archive manager in ubuntu, you'll see this structure
├── extjs
├── extjs-4.2.0
├── extjs-4.2.2
├── index.jsp
├── META-INF
├── siesta
├── tests
└── WEB-INF
我必须 CD 到我想创建一个令人讨厌的 war 文件的目录中.我认为使用 jar 的 -C 选项可能有更好的方法来做到这一点,但是当我使用jar -cvf Siesta.war -C Siesta/WebContent *"时,它没有得到相同的结果.
I have to CD into the directory that I want to create a war file of which is kind of annoying. I think there may be a better way to do it using jar's -C option but when I used "jar -cvf Siesta.war -C Siesta/WebContent *" it didn't have the same result.
编辑 2:
jar -cvf my-app.war myfolder/
为了让我的应用程序在 TomCat 上运行,我使用了以下内容:
For my application to work on TomCat, I use the following:
cd Siesta/WebContent
jar -cvf Siesta.war *
推荐答案
好吧,这里是一个已经存在的 SO,有几种关于如何创建战争文件的可能性:如何创建战争文件.
Alright here is a already existing SO with a couple of possibilities on how to create a war file: How to create war files.
我个人更喜欢 Maven,而不是任何其他构建方案 - 因为我从 ANT 迁移,所以我一开始不喜欢 Maven,但在对文档进行了大约一两天的投资之后:http://maven.apache.org/pom.html 我开始喜欢它.在您的情况下,您所需要的只是重构您的项目以满足 maven 标准文件结构,并为您的源代码创建一个最小的 Maven 项目(又名 pom.xml
文件),其中包含 war
而不是默认的 jar
.
I personaly prefer Maven over any other build scenario - Since i migrated from ANT i did not like Maven in the first place but after investing like a day or two into the documentations: http://maven.apache.org/pom.html i started to love it. In your case all you need is refactor your project to meet the maven standard file structure and create a minimum Maven project (a.k.a. pom.xml
file) for your sources that has the packaging war
instead of the default jar
.
如果您只想使用控制台,并且除了 JDK 提供的构建助手之外不使用任何构建助手,您可以使用以下内容创建您的战争:
If you want to stick with the console only and dont use any build helpers beside the ones delivered by the JDK you can create your war with something like this:
jar cvf my-app.war
看看这个:https://docs.oracle.com/javase/tutorial/deployment/jar/build.html 以获取受支持选项的完整参考.
Have a look at this: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for a complete reference of the supported options.
(注意:基于 Java 的 Web 应用程序上下文中的战争"遵循标准格式:http://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html 而技术.war"文件与重命名的 ZIP 压缩文件夹相同到.zip".)
(Note: A "war" in the context of java based web applications follows a standard format: http://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html while the technical ".war" file is just the same as a ZIP compressed folder renamed to ".zip".)
(另外请注意,我真的会花时间与 maven 取得联系,因为在那之后您了解了一个重要的构建工具,并且将能够创建 jar
、ear
>、war
等在标准过程中).
(Also note i would realy take the time to get in touch with maven since after that you know about a serious build tool and will be able to create jar
, ear
, war
etc. in a standard process).
这篇关于如何使用命令行创建 WAR 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!