eclipse Tomcat运行Maven项目/target目录下的WAR文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5687654/
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
Run WAR file in /target directory of Maven project by Tomcat
提问by ?inh H?ng Chau
I'm developing a web application with Java and Maven build system and the web server is Tomcat 7.0.12. After packaging whole project to a WAR file to \target directory by Maven build command, I have to copy it to the webapps folder of Tomcat home to run it. It's very inconvenient, especially when I modified some source files because I have to do all those things (build, copy to Tomcat, run it) again. I've research some articles about Maven, Tomcat, Eclipse on this problem, but there's no result. Could you please help me: 1. How to make Tomcat run the WAR file on target directory of project which is built by Maven command directly? No need to copy/paste the WAR file and restart Tomcat? 2. How can I configure the Tomcat to debug the web application on Eclipse? Thank you so much!
我正在使用 Java 和 Maven 构建系统开发 Web 应用程序,Web 服务器是 Tomcat 7.0.12。通过Maven build命令将整个项目打包成一个WAR文件到\target目录后,我必须将它复制到Tomcat home的webapps文件夹才能运行它。非常不方便,尤其是当我修改了一些源文件时,因为我必须再次执行所有这些操作(构建,复制到Tomcat,运行它)。关于这个问题,我研究了一些关于 Maven、Tomcat、Eclipse 的文章,但没有结果。能否请您帮帮我: 1. 如何让Tomcat 直接运行Maven 命令构建的项目目标目录下的WAR 文件?不需要复制/粘贴 WAR 文件并重新启动 Tomcat?2、如何配置Tomcat调试Eclipse上的web应用程序?非常感谢!
BTW, I've read and tried to configure Tomcat, Maven and pom file many times. But I don't know what is the exact configuration, because there are so many advices! Could you provide a particular example of configuration for me? Here is my configuration files:
顺便说一句,我已经多次阅读并尝试配置 Tomcat、Maven 和 pom 文件。但是我不知道具体的配置是什么,因为建议太多了!你能为我提供一个特定的配置示例吗?这是我的配置文件:
Tomcat tomcat-users.xml
Tomcat tomcat-users.xml
<role rolename="manager-gui"/>
<user username="admin" password="" roles="manager-gui"/>
**<role rolename="manager"/>
<user username="admin" password="" roles="manager"/>**
<role rolename="admin-gui"/>
<user username="admin" password="" roles="admin-gui"/>
Maven settings.xml
Maven 设置.xml
tomcat admin
tomcat 管理员
And the pom.xml file of project:
以及项目的 pom.xml 文件:
<build>
<finalName>my-project</finalName>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<server>tomcat</server>
<warFile> ${project.build.directory}/${project.build.finalName}.war</warFile>
</configuration>
</plugin>
</plugins>
// Other plugins
</build>
More details: If I run mvn tomcat:deploy before starting a Tomcat instance, the returned error is "Cannot invoke Tomcat manager: Connection refused: connect." Otherwise, if a Tomcat instance has been started before calling mvn tomcat:deploy, the error is "Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path=%2Fmy-project&war=..."
更多细节:如果我在启动 Tomcat 实例之前运行 mvn tomcat:deploy,返回的错误是“无法调用 Tomcat 管理器:连接被拒绝:连接”。否则,如果在调用 mvn tomcat:deploy 之前已经启动了 Tomcat 实例,则会报错“无法调用 Tomcat 管理器:服务器返回 HTTP 响应代码:403 for URL:http://localhost:8080/manager/deploy?path=% 2Fmy-project&war=..."
采纳答案by Wes
There are a couple of ways to do this.
有几种方法可以做到这一点。
The tomcat maven plugin http://mojo.codehaus.org/tomcat-maven-plugin/provides you a way to deploy directly to tomcat from maven. You must have configured your tomcat users first. See http://tomcat.apache.org/tomcat-7.0-doc/config/realm.htmlfor instructions on how to configure your users.
tomcat maven 插件http://mojo.codehaus.org/tomcat-maven-plugin/为您提供了一种从 maven 直接部署到 tomcat 的方法。您必须先配置您的 tomcat 用户。有关如何配置用户的说明,请参阅http://tomcat.apache.org/tomcat-7.0-doc/config/realm.html。
For debugging you can start tomcat with the arguments specified at http://wiki.apache.org/tomcat/FAQ/Developing
对于调试,您可以使用http://wiki.apache.org/tomcat/FAQ/Developing 中指定的参数启动 tomcat
The other way would be to configure tomcat to point to your war. See http://tomcat.apache.org/tomcat-7.0-doc/config/context.htmlparticularly the docbaseparameter.
另一种方法是配置 tomcat 以指向您的War。请参阅http://tomcat.apache.org/tomcat-7.0-doc/config/context.html特别是docbase参数。
EDIT 1Based on the revised question http 403 is a forbidden this means you didn't authenticate correctly.
编辑 1基于修改后的问题 http 403 是禁止的,这意味着您没有正确进行身份验证。
Change your tomcat-users.xml to look as follows:
将您的 tomcat-users.xml 更改为如下所示:
You need to add a refrence to the username/password in your pom.xml. You settings.xml must contain (based on your configuration)
您需要在 pom.xml 中添加对用户名/密码的引用。您的 settings.xml 必须包含(基于您的配置)
<server>
<id>tomcat</id>
<username>admin</username>
<password></password>
</server>
Theres a good page http://www.avajava.com/tutorials/lessons/how-do-i-deploy-a-maven-web-application-to-tomcat.htmlthat explains it more fully. However the information may be out of date.
有一个很好的页面http://www.avajava.com/tutorials/lessons/how-do-i-deploy-a-maven-web-application-to-tomcat.html更全面地解释了它。但是,这些信息可能已经过时。
回答by Chi Kim
I ran into exactly the same issue.
我遇到了完全相同的问题。
I suppose it is eclipse-tomcat plugin that is doing this. And I have no idea why it works differently in tomcat 7 opposed to tomcat 6 which I didn't have problem with.
我想是 eclipse-tomcat 插件在做这个。而且我不知道为什么它在 tomcat 7 中的工作方式与我没有问题的 tomcat 6 不同。
Anyways, what I did was to have maven build into the /web-app/WEB-INF directory
无论如何,我所做的是将 maven 构建到 /web-app/WEB-INF 目录中
<build>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
...
</build>
After this config, eclipse:eclipse should yield a proper eclipse project/classpath files resources:resources shoudl work accordingly as well.
在这个配置之后, eclipse:eclipse 应该产生一个正确的 eclipse 项目/类路径文件 resources:resources 也应该相应地工作。