如何从 Eclipse 在 Tomcat 中运行 Maven 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/921380/
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
How to run a Maven Project In Tomcat From Eclipse
提问by
I am running a multi level maven project. It has webapps packaged as WAR, which depend on components packaged as JAR files
我正在运行一个多级 Maven 项目。它有打包成 WAR 的 webapps,它依赖于打包成 JAR 文件的组件
Most of the code changes that i do belong to those JAR files.
我所做的大部分代码更改都属于这些 JAR 文件。
To test the changes, i need to repackage the JAR and the WAR and deploy externally in tomcat.
为了测试更改,我需要重新打包 JAR 和 WAR 并在 tomcat 中进行外部部署。
I tried using sysdeo-tomcat-maven-eclipse plugin, but it did not work as expected.
我尝试使用 sysdeo-tomcat-maven-eclipse 插件,但它没有按预期工作。
Please advise if anyone has a solution for this problem
请告知是否有人对此问题有解决方案
Thanks Gaurav
谢谢高拉夫
回答by Martin
Using the Maven Eclipse Integration plugin
使用 Maven Eclipse 集成插件
I you are experiencing problems I've found it helpful to try the command line tool mvnfor debugging. As a rule of thumb, if it builds without errors command line, then you can run it in Eclipse (but tomcat has problems also, so you might need to clean work directory and clear caches every once in a while).
如果您遇到问题,我发现尝试使用命令行工具mvn进行调试很有帮助。根据经验,如果命令行构建没有错误,那么您可以在 Eclipse 中运行它(但 tomcat 也有问题,因此您可能需要每隔一段时间清理工作目录并清除缓存)。
Other than that I really recommend using the Maven Eclipse Integration plugin! I use the version from Sonatypes plugin repo http://m2eclipse.sonatype.org/sites/m2e. There is a newer integration on the eclipse repo, but it works differently and I have not yet had time to investigate it's uses yet.
除此之外,我真的建议使用 Maven Eclipse 集成插件!我使用来自 Sonatypes 插件仓库http://m2eclipse.sonatype.org/sites/m2e的版本。eclipse repo 上有一个更新的集成,但它的工作方式不同,我还没有时间调查它的用途。
If your project is modular, it may help to setup the root project as a simple project (no Java , no Java EE). Then import the sub-projects as simple Java projects (again no Java EE). You can still activate dependency management and run the build targets through the project.
如果您的项目是模块化的,那么将根项目设置为一个简单的项目(没有 Java ,没有 Java EE)可能会有所帮助。然后将子项目作为简单的 Java 项目导入(同样没有 Java EE)。您仍然可以激活依赖项管理并通过项目运行构建目标。
You only need to select the submodules required by your targeted project(s) by dependencies.
您只需要通过依赖项选择目标项目所需的子模块。
For any project that builds war files activate facets for java ee and configure context root under further configuration available(this can only be done once, why the option is missing in the screenshot, so do it right).
对于任何构建 war 文件的项目,激活 java ee 的 facets 并在可用的进一步配置下配置上下文根(这只能完成一次,为什么屏幕截图中缺少该选项,所以请正确操作)。
Then setup deployment assembly for your facated projects. All sibling projects (active in you workspace) should be added to deployment assembly and you also need to add the build path entryfor maven dependenciesto the deployment assembly. Once this is set up you can use the Run on server directive to boot your web apps.
然后为您的facated 项目设置部署程序集。所有兄弟项目(活跃在你工作区)应装配添加到部署,您还需要添加的构建路径条目的Maven依赖组件的部署。设置完成后,您可以使用 Run on server 指令来启动您的 Web 应用程序。
I find it useful to always stay in pure java mode when coding maven projects. Any smart views in Eclipse only obscures what happens behind the scene, you get access to the specific editors by activating facated projects anyway!
我发现在编写 Maven 项目时始终保持纯 Java 模式很有用。Eclipse 中的任何智能视图只会掩盖幕后发生的事情,无论如何,您都可以通过激活facated 项目来访问特定的编辑器!
回答by David Rabinowitz
I use the eclipse webtools plugin, and run my Tomcat from it. It has a republish method that publishes the jars as well (if not, you just clean the directory and republish).
我使用 eclipse webtools 插件,并从中运行我的 Tomcat。它有一个 republish 方法也可以发布 jars(如果没有,您只需清理目录并重新发布)。
In the pom file, you should have the following lines so that the webtools will be supported automatically
在 pom 文件中,您应该有以下几行,以便自动支持 webtools
<build>
...
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpversion>1.5</wtpversion>
...
</configuration>
</plugin>
...
</plugins>
...
</build>
回答by Martin
Using the Maven Eclipse Integration plugin and Web Runner
使用 Maven Eclipse 集成插件和 Web Runner
Setup a facated project (already described in another answer), use the latest Maven Eclipse Integration plugin. This should be located at the update site of your eclipse version under collaboration. In my case Indigo - http://download.eclipse.org/releases/indigo
(/Collaboration/Maven Integration for Eclipse).
设置一个 facated 项目(已在另一个答案中描述),使用最新的 Maven Eclipse 集成插件。这应该位于协作下的 eclipse 版本的更新站点。就我而言,Indigo - http://download.eclipse.org/releases/indigo
(Eclipse 的/Collaboration/Maven 集成)。
As an alternative to the built in server which often fails on updating workspace dependencies in the deployment assembly (for tomcat) I would recommend using Sonatype Web Application Launcher Webbywhich is similar to the maven directive jetty:run of the maven jetty plugin. You can find the plugin at sonatypes update site for the plugin M2e Webby - http://m2eclipse.sonatype.org/sites/m2e-webby/
. Once this plugin is installed you can Run configurationand run the web-app from an auto build located in {project}/target/m2e-webby
.
作为内置服务器的替代方案,在更新部署程序集中的工作区依赖项时经常失败(对于 tomcat),我建议使用 Sonatype Web Application Launcher Webby,它类似于 maven 指令 jetty:run 的 maven jetty 插件。您可以在插件 M2e Webby - 的 sonatypes 更新站点上找到该插件http://m2eclipse.sonatype.org/sites/m2e-webby/
。安装此插件后,您可以运行配置并从位于{project}/target/m2e-webby
.
I recommend configuring one of the (default) embedded jetty-containers as provider for the servlet-api. If specific runtimes have to be tested, an external container can easily be configured by browsing to a server home directory and selecting the appropriate provider .
我建议将(默认)嵌入式码头容器之一配置为 servlet-api 的提供者。如果必须测试特定的运行时,可以通过浏览到服务器主目录并选择适当的提供程序来轻松配置外部容器。
The disadvantages I've encountered are quite minor:
我遇到的缺点非常小:
- Web applications must be manually shut down, there is no "restart".
- Each web application has to run on a separate port, they do not share container instance.
- Web 应用程序必须手动关闭,没有“重新启动”。
- 每个 Web 应用程序都必须在单独的端口上运行,它们不共享容器实例。
回答by surajz
You get 'Unable to read TLD "META-INF/c.tld" from JAR file "file:..g/workspace/.metadata/.plugins' error with the current version of m2eclipse plugin with the above configuration.
对于具有上述配置的当前版本的 m2eclipse 插件,您会收到“无法从 JAR 文件“文件:..g/workspace/.metadata/.plugins”中读取 TLD“META-INF/c.tld”错误。
You need to delete servlet-api.jar and jsp-api.jar from the deployed projects lib/ folder to fix the above error, and setup custom build configuration so that the 2 files are not copied to the deployment directory.
您需要从已部署的项目 lib/ 文件夹中删除 servlet-api.jar 和 jsp-api.jar 以修复上述错误,并设置自定义构建配置,以便不会将这两个文件复制到部署目录。