简体   繁体   English

使用Maven将Tomcat集成到Eclipse中

[英]Tomcat integration in Eclipse with maven

I'm trying to integrate Maven to my web project. 我正在尝试将Maven集成到我的Web项目中。 I want t run project with mvn tomcat7:run, but when I do it maven creates new tomcat configuration in MyProject/target/tomcat and when I'm trying to open it on localhost and there is 404 error. 我想用mvn tomcat7:run运行项目,但是当我这样做时,maven在MyProject / target / tomcat中创建了新的tomcat配置,并且当我尝试在localhost上打开它时出现404错误。 Can anybody describe me what I should do to configurate maven to use local preconfigured tomcat instance? 谁能形容我应该如何配置Maven以使用本地预配置的tomcat实例?

Maven creates a Tomcat configuration locally with mvn tomcat7:run because you are running the webapp out of the /target directory and the aim is to keep the running webapp isolated from any external configuration. Maven使用mvn tomcat7:run在本地创建Tomcat配置,因为您正在/target目录之外运行webapp,目的是使运行的webapp与任何外部配置隔离。 For example if you required custom Tomcat configuration applying then Maven would need to modify the Tomcat conf beforehand. 例如,如果您需要应用自定义Tomcat配置,则Maven将需要事先修改Tomcat conf。 This might not be possible due to file system permission or could affect other webapps. 由于文件系统的许可,这可能无法实现,或可能影响其他Web应用程序。 So Maven copies the config locally, applies any custom configuration and starts Tomcat via the tomcat7-maven-plugin . 因此,Maven在本地复制配置,应用任何自定义配置,然后通过tomcat7-maven-plugin启动Tomcat。

One option is to look at the maven-cargo-plugin which can deploy a .war built with Maven into an existing running Tomcat installation. 一种选择是查看maven-cargo-plugin ,它可以将使用Maven构建的.war部署到现有的正在运行的Tomcat安装中。 With the correct configuration , instead or using mvn tomcat7:run you would use mvn cargo:redeploy to remove a running webapp and replace it with your freshly built webapp. 如果使用正确的配置 ,或者使用mvn tomcat7:run ,则可以使用mvn cargo:redeploy删除正在运行的Webapp,并将其替换为新构建的Webapp。

As an alternative Eclipse can be configured to start a Tomcat instance in which to run the webapp in. If you change the Eclipse project type to a Dynamic Web Project and then add a Tomcat instance to Eclipse by doing: 或者,可以将Eclipse配置为启动要在其中运行webapp的Tomcat实例。如果将Eclipse项目类型更改为Dynamic Web Project ,然后通过执行以下操作将Tomcat实例添加到Eclipse:

  • Window → Preferences → Server → Runtime Environments 窗口→首选项→服务器→运行时环境
  • Add → Apache Tomcat v7.0 添加→Apache Tomcat v7.0

and point it do the Tomcat you downloaded. 并指向您下载的Tomcat。 Eclipse will use this external Tomcat as a template to create an internal Tomcat configuration for your webapp. Eclipse将使用这个外部 Tomcat作为模板来创建你的webapp 内部 Tomcat配置。 Lastly, the webapp needs to be configured to deploy the correct resources and libraries into Tomcat. 最后,需要对Web应用程序进行配置,以将正确的资源和库部署到Tomcat中。 By right clicking on the project root → Properties → Deployment Assembly, alter the list to match your resource locations. 通过右键单击项目根目录→属性→部署程序集,更改列表以匹配您的资源位置。 A standard set-up might look something like 标准设置可能看起来像

/src/main/java       |   /WEB-INF/classes
/src/main/resources  |   /WEB-INF/classes
/src/main/webapp     |   /
Maven Dependencies   |   /WEB-INF/lib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM