Java 将项目从 Eclipse 部署到 Tomcat

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

deploy project to Tomcat from Eclipse

javaeclipsetomcatmaven-2

提问by Dónal

I'm trying to deploy a multi-module Maven project from Eclipse to a local Tomcat. The project has the following structure:

我正在尝试将多模块 Maven 项目从 Eclipse 部署到本地 Tomcat。该项目具有以下结构:

root [packaging: pom]
|
|--- domain [packaging: jar]
|
|--- manager [packaging: jar]
|
|--- web [packaging: war]

I import all the projects into Eclipse, then add the "Dynamic Web Module" facet to the web project

我将所有项目导入 Eclipse,然后将“动态 Web 模块”方面添加到 Web 项目中

enter image description here

在此处输入图片说明

In the Runtimes tab of the same dialog, I configure the project to use a (local) Apache Tomcat v6.0 runtime.

在同一个对话框的 Runtimes 选项卡中,我将项目配置为使用(本地)Apache Tomcat v6.0 运行时。

I then right-click on the project and choose "Run As -> Run on Server" from the menu. In the following dialog I (again?) choose "Tomcat v6.0 Server"

然后我右键单击该项目并从菜单中选择“运行方式 -> 在服务器上运行”。在下面的对话框中,我(再次?)选择“Tomcat v6.0 Server”

enter image description here

在此处输入图片说明

In the following dialog, I add the web project to the list of configured projects

在以下对话框中,我将 Web 项目添加到已配置项目列表中

enter image description here

在此处输入图片说明

When I click "Finish", it appears from the console messages that the server has started, but when I go to http://localhost:8080/web/in a browser I get a 404. I also tried http://localhost:8080/and that also returns a 404, what am I doing wrong?

当我单击“完成”时,从控制台消息中显示服务器已启动,但是当我在浏览器中访问http://localhost:8080/web/ 时,我得到一个 404。我也尝试过http://localhost :8080/并且也返回 404,我做错了什么?

回答by Vineet Reynolds

I also tried http://localhost:8080/and that also returns a 404.

我也试过http://localhost:8080/并且也返回 404。

I would infer that either Tomcat is not listening on port 8080, for the behavior is to display the contents of $CATALINA_HOME/webapps/ROOT/index.htmlas the welcome page. You might want to verify that Tomcat is configured to listen on port 8080 in the first place. The $CATALINA_HOME/conf/server.xmlfile should have entry similar to one listed below:

我会推断 Tomcat 没有侦听端口 8080,因为行为是将 的内容显示$CATALINA_HOME/webapps/ROOT/index.html为欢迎页面。您可能首先要验证 Tomcat 是否配置为侦听端口 8080。该$CATALINA_HOME/conf/server.xml文件应具有类似于下面列出的条目:

<Server port="8005" shutdown="SHUTDOWN">
    ...
    <Service name="Catalina">
        ...
        <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443" />

If you've got this right, but you are seeing a 404 response, then obviously something is wrong with the configuration. Either $CATALINA_HOME/webapps/ROOTdoes not exist, or something else which you might determine by looking at the logs in the $CATALINA_HOME/logsdirectory.

如果您做对了,但您看到的是 404 响应,那么显然配置有问题。要么$CATALINA_HOME/webapps/ROOT不存在,要么您可以通过查看$CATALINA_HOME/logs目录中的日志来确定其他内容。

When I click "Finish", it appears from the console messages that the server has started, but when I go to http://localhost:8080/web/in a browser I get a 404

当我单击“完成”时,从控制台消息中显示服务器已启动,但是当我在浏览器中访问http://localhost:8080/web/ 时,我收到 404

Assuming that Tomcat has been configured successfully, this described behavior is likely if you do not have a welcome file listed in web.xml. The structure of the welcome-file-listis similar to the one listed below:

假设 Tomcat 已成功配置,如果您没有在web.xml. 的结构welcome-file-list类似于下面列出的结构:

<web-app>
...
    <welcome-file-list>
        <welcome-file>somePage.html</welcome-file>
    </welcome-file-list>
...
</web-app>

Quite obviously, you'll need to have valid HTML in the welcome page. You could also have a JSP page to act as the welcome page.

很明显,您需要在欢迎页面中有有效的 HTML。您还可以有一个 JSP 页面作为欢迎页面。

Maven integration with Eclipse

Maven 与 Eclipse 的集成

I'm putting this in a separate section, as I'm unsure if your Eclipse project is setup correctly in the first place. Typically one would use either of m2eclipseor the maven-eclipse-pluginto configure a WTP project in Eclipse.

我把它放在一个单独的部分,因为我不确定你的 Eclipse 项目是否首先设置正确。通常,可以使用m2eclipsemaven-eclipse-plugin来配置 Eclipse 中的 WTP 项目。

If you have added the Maven nature (using the m2eclipse plug-in) to the project, then you must know that m2eclipse has the tendency to revert any changes that might conflict the ones that it generates. For instance, m2eclipse would set the version Java facet to the sourceversion specified for the maven-compiler-pluginin your pom.xml.

如果您已将 Maven 特性(使用 m2eclipse 插件)添加到项目中,那么您必须知道 m2eclipse 倾向于还原任何可能与其生成的更改冲突的更改。例如,m2eclipse 会将版本 Java facet 设置sourcemaven-compiler-plugin在 pom.xml 中为 指定的版本。

Also, it might be important to run the packagephase, before deploying the application on the server. I've been tripped by this behavior on some occasions.

此外,package在服务器上部署应用程序之前运行该阶段可能很重要。在某些情况下,我被这种行为绊倒了。

回答by Dónal

Success!!!

成功!!!

When I added the dynamic module Eclipse incorrectly created a WebContent directory. I deleted this directory and made the following change to the file in the .settingsdirectory named org.eclipse.wst.common.component

当我添加动态模块时,Eclipse 错误地创建了一个 WebContent 目录。我删除了这个目录并对.settings名为的目录中的文件进行了以下更改org.eclipse.wst.common.component

<wb-resource deploy-path="/" source-path="/src/main/webapp"/>
<!--
<wb-resource deploy-path="/" source-path="/WebContent"/>
-->