使用 Eclipse 和 Maven 调试打包为 WAR 的 Java Web 应用程序的最佳方法?

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

Best way to debug Java web application packaged as a WAR using Eclipse and Maven?

javaeclipsedebuggingmaven-2

提问by HDave

I've not built a Java web application before, but I have it complete enough to test and Maven is building my WAR file just fine. It is a multi-module Maven project and the dependent modules all have their JAR files in the WEB-INF/lib directory of the WAR.

我之前没有构建过 Java Web 应用程序,但我已经完成了足够的测试,并且 Maven 正在构建我的 WAR 文件就好了。它是一个多模块的 Maven 项目,依赖的模块在 WAR 的 WEB-INF/lib 目录中都有它们的 JAR 文件。

So everything seems fine, but how to debug? I know how to (from the command line) run the WAR in Tomcat on my machine. I also think I know how to set up and run the Maven Jetty plugin from the command line as well. But how best to debug...with all the break points and variable inspection I love with Eclipse?

所以一切看起来都很好,但是如何调试呢?我知道如何(从命令行)在我的机器上的 Tomcat 中运行 WAR。我还认为我知道如何从命令行设置和运行 Maven Jetty 插件。但是如何最好地调试......使用我喜欢 Eclipse 的所有断点和变量检查?

Is there some kind of launch configuration I should create, or do I attach the debugger remotely? Is there something in Eclipse that can help...like a plugin?

我应该创建某种启动配置,还是远程连接调试器?Eclipse 中有什么东西可以帮助……比如插件?

采纳答案by Henryk Konsek

If you run your WAR with tomcat/jetty plugin pass debug options to the Maven:

如果您使用 tomcat/jetty 插件运行 WAR,请将调试选项传递给 Maven:

export MAVEN_OPTS="-Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" mvn tomcat:run

If you run your WAR using the regular Tomcat, just run it with JPDA (debugger) support enabled:

如果您使用常规 Tomcat 运行 WAR,只需在启用 JPDA(调试器)支持的情况下运行它:

$TOMCAT_HOME/bin/catalina.sh jpda start

Default port for Tomcat 6 JPDA is 8000.

Tomcat 6 JPDA 的默认端口是 8000。

Now connect with the Eclipse (Debug -> Remote Java Application) to the port 8000 and enjoy your fine debugging session.

现在将 Eclipse(调试 -> 远程 Java 应用程序)连接到端口 8000 并享受您的调试会话。

回答by YoK

To enable debugging through eclipse :

要通过 Eclipse 启用调试:

I pass following to Tomcat startup:

我将以下内容传递给 Tomcat 启动:

-Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=9999,suspend=n

Then through Eclipse do Remote Debug.

然后通过Eclipse做Remote Debug。

  • Goto Debug Menu > Debug Configuration

  • Scroll Down to Select Remote Java Application

  • rt click to create new
    configuration
  • Select connection type as Standard (Socket Attach) and add hostname and port.
  • 转到调试菜单 > 调试配置

  • 向下滚动以选择远程 Java 应用程序

  • rt 单击以创建新
    配置
  • 选择连接类型为标准(套接字连接)并添加主机名和端口。

To start debugging simple open it when server is running.---

要开始调试,请在服务器运行时打开它。---

回答by Pascal Thivent

Is there some kind of launch configuration I should create, or do I attach the debugger remotely? Is there something in Eclipse that can help...like a plugin?

我应该创建某种启动配置,还是远程连接调试器?Eclipse 中有什么东西可以帮助……比如插件?

With m2eclipse(and the Maven Integration for WTPthat you installfrom the Extras), you could use the WTP and start your app in debug mode.

随着m2eclipse的(和Maven的集成WTP安装从其他方案),您可以使用WTP和调试模式启动应用程序。

As an alternative, you could connect a remote debugger to a Jetty. See Debugging with the Maven Jetty Plugin in Eclipse.

作为替代方案,您可以将远程调试器连接到 Jetty。请参阅在 Eclipse 中使用 Maven Jetty 插件进行调试

回答by ahmehri

A better way of adding the debugging options to $MAVEN_OPTSoption, and thus not merging them with the other existing options (if you want to start your app not in the debug mode, you have to remove those options again), is to use the Maven out of the box debugger mvnDebug, located in its bin directory, this way mvnDebug jetty:run. This will execute your app in debug mode and what's remaining is attaching your debugger.

将调试选项添加到$MAVEN_OPTSoption的更好方法是使用 Maven out的框调试器mvnDebug,位于其 bin 目录中,这样mvnDebug jetty:run. 这将在调试模式下执行您的应用程序,剩下的就是附加您的调试器。