在 Tomcat / Eclipse 中部署 WAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4269133/
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-09-10 19:46:10  来源:igfitidea点击:

Deploying a WAR in Tomcat / Eclipse

eclipsetomcatdeploymentwar

提问by Lo?c Guillois

I use Tomcat 6.0 and Eclipse 3.0 under Linux and I try to deploy a WAR in Tomcat. The problem is that the server is managed by Eclipse and I have some Eclipse project deployed. I tried to modify the server.xml file then launch Tomcat via Eclipse but it doesn't work:

我在 Linux 下使用 Tomcat 6.0 和 Eclipse 3.0,并尝试在 Tomcat 中部署 WAR。问题是服务器由 Eclipse 管理,我部署了一些 Eclipse 项目。我尝试修改 server.xml 文件,然后通过 Eclipse 启动 Tomcat 但它不起作用:

Could not load the Tomcat server configuration at /Servers/Tomcat v6.0 Server at localhost-config. The configuration may be corrupt or incomplete.

无法在 /Servers/Tomcat v6.0 Server at localhost-config 加载 Tomcat 服务器配置。配置可能已损坏或不完整。

I tried to extract the war in the webapps directory but the webapp is still inaccessible.

我试图在 webapps 目录中提取War,但 webapp 仍然无法访问。

What is the best practice to deploy a War ?

部署War的最佳实践是什么?

回答by Yoichi

Yes, in a way, you can deploy a war in the dev mode.

是的,在某种程度上,您可以在开发模式下部署War。

I have the same problem.

我也有同样的问题。

I have an Eclipse webapp project, which Eclipse deploys to an instance of Tomcat run by Eclipse, so I can hot-edit the project.

我有一个 Eclipse webapp 项目,Eclipse 将其部署到由 Eclipse 运行的 Tomcat 实例,因此我可以热编辑该项目。

This Web project needs to use resources published by another webapp that has to be run within the same instance of Tomcat. The other webapp is a completed project by someone else, so it is already in a war form.

这个 Web 项目需要使用另一个 webapp 发布的资源,这个 webapp 必须在同一个 Tomcat 实例中运行。另一个webapp是别人完成的项目,所以已经处于War状态。

I needed to File->Import the war as an Eclipse project and let Eclipse deploy it to the same instance of Eclipse, in order to run it in the same instance of Tomcat in which my webapp also runs.

我需要 File->Import the war 作为 Eclipse 项目,并让 Eclipse 将它部署到 Eclipse 的同一个实例,以便在我的 webapp 也运行的 Tomcat 的同一个实例中运行它。

The problem is that some wars work this way but some others do not, while all of them work perfectly fine in a stand-alone Tomcat (started by startup.sh). I can't figure out why.

问题是有些War以这种方式工作,但有些则不然,而所有这些War在独立的 Tomcat(由 startup.sh 启动)中都可以正常工作。我不明白为什么。

回答by Luke

Tomcat behaves differently in development and production mode. When you develop your webapp in Eclipse there is no reason to deploy a WAR file of your application as a WAR during development.

Tomcat 在开发和生产模式下的行为不同。在 Eclipse 中开发 Web 应用程序时,没有理由在开发期间将应用程序的 WAR 文件部署为 WAR。

Just go to the "servers" view and add a new server (you should already have done this otherwise you could not create your Dynamic Web project). In the server view you should see the server you created (Tomcat at localhost or something similar) just right click it and go to the Add and Remove section. Here you can add and remove the Dynamic Web projects you created in Eclipse. Once you added your project, all you have to do is click the green start button in the servers view and your app should be available in at localhost:8080/mycontext.

只需转到“服务器”视图并添加一个新服务器(您应该已经这样做了,否则您将无法创建您的动态 Web 项目)。在服务器视图中,您应该会看到您创建的服务器(本地主机上的 Tomcat 或类似的东西),只需右键单击它并转到“添加和删除”部分。您可以在此处添加和删除您在 Eclipse 中创建的动态 Web 项目。添加项目后,您所要做的就是单击服务器视图中的绿色启动按钮,您的应用程序应该可以在localhost:8080/mycontext.

When you're done building your app just right click the project and go the the Export section in the menu. You should be able to export a WAR file. Once you have your WAR file you can upload and deploy that on a Tomcat instance that is NOT tied to Eclipse running in dev mode.

完成构建应用程序后,只需右键单击项目并转到菜单中的导出部分。您应该能够导出 WAR 文件。一旦您拥有 WAR 文件,您就可以将其上传并部署到与在开发模式下运行的 Eclipse 无关的 Tomcat 实例上。

回答by Isaac Israel Vazquez Paz

This is old but is one of the first answers in google search.

这是旧的,但它是谷歌搜索中的第一个答案。

You can import the war file:

您可以导入war文件:

A Web Archive (WAR) file is a portable, packaged Web application that you can import into your workspace.

Web Archive (WAR) 文件是一个可移植的打包 Web 应用程序,您可以将其导入到您的工作区中。

Before importing a WAR file, you should first determine if the WAR file contains needed Java? source files. When importing a WAR file into an existing Web project, the imported Web deployment descriptor files are either not changed or overwritten by the ones included in the imported WAR file, based on your response to the prompt that is provided. In either case, this action does notrepresent a merging of the two sets of deployment descriptors.

在导入 WAR 文件之前,您应该首先确定 WAR 文件是否包含所需的 Java?源文件。将 WAR 文件导入现有 Web 项目时,根据您对提供的提示的响应,导入的 Web 部署描述符文件不会更改或被导入的 WAR 文件中包含的文件覆盖。在这两种情况下,这个动作并不能代表一个合并两套部署描述符的。

To import the Web project resources in a WAR file into your workspace, complete the following steps:

要将 WAR 文件中的 Web 项目资源导入您的工作区,请完成以下步骤:

  1. Select File > Import .
  2. In the Import dialog, select WAR file and then click Next.
  3. Locate the WAR file that you want to import using the Browse button.
  4. The wizard assumes you want to create a new Web project with the same name as the WAR file. If you accept this choice, the project will be created with the same servlet version as specified by the WAR file and in the same location. If you want to override these settings, you can click Newand specify your new settings in the Dynamic Web Project wizard.
  5. Click Finish to populate the Web project.
  1. 选择文件 > 导入。
  2. 在导入对话框中,选择 WAR 文件,然后单击下一步。
  3. 使用浏览按钮找到要导入的 WAR 文件。
  4. 该向导假定您要创建一个与 WAR 文件同名的新 Web 项目。如果您接受此选择,将使用 WAR 文件指定的相同 servlet 版本并在相同位置创建项目。如果要覆盖这些设置,可以单击新建并在动态 Web 项目向导中指定新设置。
  5. 单击完成以填充 Web 项目。

Source: http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Ftwimpwar.html

来源:http: //help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Ftwimpwar.html

回答by Alex R

If all you have is a binary WAR (no source code), it cannot be installed within Eclipse. This can happen in certain scenarios outside of normal development workflows. Here's the work-around solution:

如果您拥有的只是二进制 WAR(无源代码),则无法在 Eclipse 中安装它。这可能发生在正常开发工作流程之外的某些场景中。这是解决方法:

  1. Launch another instance of Tomcat (outside Eclipse).
  2. Modify the tomcat-users.xmlfile to enable admin
  3. Go to http://localhost:8080/manager/html
  4. Scroll down to WAR file to deploy
  5. Click Choose File(next to Select WAR file to upload) and click Deploy.
  1. 启动另一个 Tomcat 实例(在 Eclipse 之外)。
  2. 修改tomcat-users.xml文件以启用管理员
  3. http://localhost:8080/manager/html
  4. 向下滚动到要部署的 WAR 文件
  5. 单击选择文件(在选择要上传的 WAR 文件旁边)并单击部署