Java 在 Maven 中调试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2935375/
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
Debugging in Maven?
提问by aduric
Is it possible to launch a debugger such as jdb from Maven? I have a pom.xmlfile that compiles the project successfully. However, the program hangs somewhere and I would really like to launch jdb or an equivalent debugger to see what's happening.
是否可以从 Maven 启动调试器,例如 jdb?我有一个成功编译项目的pom.xml文件。但是,该程序挂在某处,我真的很想启动 jdb 或等效的调试器来查看发生了什么。
I compile using mvn compile
and launch using:
我编译使用mvn compile
和启动使用:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
I was expecting something like:
我期待这样的事情:
mvn exec:jdb -Dexec.mainClass="com.mycompany.app.App"
to launch the debugger but, as usual, my expectations are incongruent with maven's philosophy.
启动调试器,但像往常一样,我的期望与 maven 的理念不一致。
Also, I couldn't find any documentation (on Maven's website or google) to describe how debugging works. I suspect that I have to use some plugin.
另外,我找不到任何文档(在 Maven 的网站或谷歌上)来描述调试是如何工作的。我怀疑我必须使用一些插件。
采纳答案by Samuel Yung
Just as Brian said, you can use remote debugging:
正如布赖恩所说,你可以使用远程调试:
mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 com.mycompany.app.App"
Then in your eclipse, you can use remote debugging and attach the debugger to localhost:1044.
然后在您的 Eclipse 中,您可以使用远程调试并将调试器附加到 localhost:1044。
回答by Brian Agnew
Why not use the JPDAand attach to the launched process from a separate debugger process ? You should be able to specify the appropriate options in Maven to launch your process with the debugging hooks enabled. This articlehas more information.
为什么不使用JPDA并从单独的调试器进程附加到启动的进程?您应该能够在 Maven 中指定适当的选项以在启用调试挂钩的情况下启动您的进程。这篇文章有更多信息。
回答by Pascal Thivent
If you are using Maven 2.0.8+, run the mvnDebug
command in place of mvn
and attach a debugger on port 8000.
如果您使用的是 Maven 2.0.8+,请运行该mvnDebug
命令代替mvn
并在端口 8000 上附加调试器。
For Maven <2.0.8, uncomment the following line in your %M2_HOME%/bin/mvn.bat
(and maybe save the modified version as mvnDebug.bat
):
对于 Maven <2.0.8,在您的%M2_HOME%/bin/mvn.bat
(可能将修改后的版本另存为mvnDebug.bat
)中取消注释以下行:
@REM set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
More details in MNG-2105and Dealing with Eclipse-based IDE.
MNG-2105和处理基于 Eclipse 的 IDE 中的更多详细信息。
回答by flurdy
I use the MAVEN_OPTS option, and find it useful to set suspend to "suspend=y" as my exec:java programs tend to be small generators which are finished before I have manage to attach a debugger.... :) With suspend on it will wait for a debugger to attach before proceding.
我使用 MAVEN_OPTS 选项,发现将挂起设置为“挂起 = y”很有用,因为我的 exec:java 程序往往是小型生成器,在我设法附加调试器之前就完成了...... :) 挂起在继续之前,它将等待调试器附加。
回答by magarciaschopohl
If you are using Netbeans, there is a nice shortcut to this.
Just define a goal exec:java
and add the property jpda.listen=maven
如果您使用的是 Netbeans,有一个很好的快捷方式。只需定义一个目标exec:java
并添加属性jpda.listen=maven
Tested on Netbeans 7.3
在 Netbeans 7.3 上测试
回答by RickHigh
I thought I would expand on these answers for OSX and Linux folks (not that they need it):
我想我会为 OSX 和 Linux 人员扩展这些答案(不是他们需要它):
I prefer to use mvnDebugtoo. But after OSX maverick destroyed my Java dev environment, I am starting from scratch and stubbled upon this post, and thought I would add to it.
我也更喜欢使用mvnDebug。但是在 OSX 特立独行破坏了我的 Java 开发环境之后,我从头开始,并在这篇文章中发了疯,并认为我会添加它。
$ mvnDebug vertx:runMod
-bash: mvnDebug: command not found
DOH! I have not set it up on this box after the new SSD drive and/or the reset of everything Java when I installed Maverick.
哦!当我安装 Maverick 时,在新的 SSD 驱动器和/或所有 Java 重置之后,我还没有在这个盒子上设置它。
I use a package manager for OSX and Linux so I have no idea where mvn really lives. (I know for brief periods of time.. thanks brew.. I like that I don't know this.)
我使用 OSX 和 Linux 的包管理器,所以我不知道 mvn 到底在哪里。(我知道一小段时间..谢谢 brew.. 我喜欢我不知道这个。)
Let's see:
让我们来看看:
$ which mvn
/usr/local/bin/mvn
There you are... you little b@stard.
你在那里......你这个小b@stard。
Now where did you get installed to:
现在你在哪里安装到:
$ ls -l /usr/local/bin/mvn
lrwxr-xr-x 1 root wheel 39 Oct 31 13:00 /
/usr/local/bin/mvn -> /usr/local/Cellar/maven30/3.0.5/bin/mvn
Aha! So you got installed in /usr/local/Cellar/maven30/3.0.5/bin/mvn. You cheeky little build tool. No doubt by homebrew...
啊哈!所以你安装在/usr/local/Cellar/maven30/3.0.5/bin/mvn。你这个厚颜无耻的小构建工具。毫无疑问,自制软件...
Do you have your little buddy mvnDebug with you?
你有你的小伙伴 mvnDebug 吗?
$ ls /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug
/usr/local/Cellar/maven30/3.0.5/bin/mvnDebug
Good. Good. Very good. All going as planned.
好的。好的。非常好。一切都按计划进行。
Now move that little b@stard where I can remember him more easily.
现在把那个小 b@stard 移到我更容易记住他的地方。
$ ln -s /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug /usr/local/bin/mvnDebug
ln: /usr/local/bin/mvnDebug: Permission denied
Darn you computer... You will submit to my will. Do you know who I am? I am SUDO! BOW!
该死的电脑......你会服从我的意愿。你知道我是谁吗?我是苏多!弓!
$ sudo ln -s /usr/local/Cellar/maven30/3.0.5/bin/mvnDebug /usr/local/bin/mvnDebug
Now I can use it from Eclipse (but why would I do that when I have IntelliJ!!!!)
现在我可以从 Eclipse 使用它(但是当我有 IntelliJ 时我为什么要这样做!!!!)
$ mvnDebug vertx:runMod
Preparing to Execute Maven in Debug Mode
Listening for transport dt_socket at address: 8000
Internally mvnDebug uses this:
在内部 mvnDebug 使用这个:
MAVEN_DEBUG_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE \
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
So you could modify it (I usually debug on port 9090).
所以你可以修改它(我通常在端口 9090 上调试)。
This blog explains how to setup Eclipse remote debugging (shudder)
这篇博客解释了如何设置 Eclipse 远程调试(不寒而栗)
http://javarevisited.blogspot.com/2011/02/how-to-setup-remote-debugging-in.html
http://javarevisited.blogspot.com/2011/02/how-to-setup-remote-debugging-in.html
Ditto Netbeans
同上网豆
https://blogs.oracle.com/atishay/entry/use_netbeans_to_debug_a
https://blogs.oracle.com/atishay/entry/use_netbeans_to_debug_a
Ditto IntelliJ http://www.jetbrains.com/idea/webhelp/run-debug-configuration-remote.html
同上 IntelliJ http://www.jetbrains.com/idea/webhelp/run-debug-configuration-remote.html
Here is some good docs on the -Xdebug command in general.
这里有一些关于 -Xdebug 命令的好文档。
http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html
http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html
"-Xdebug enables debugging capabilities in the JVM which are used by the Java Virtual Machine Tools Interface (JVMTI). JVMTI is a low-level debugging interface used by debuggers and profiling tools. With it, you can inspect the state and control the execution of applications running in the JVM."
"-Xdebug 启用 Java 虚拟机工具接口 (JVMTI) 使用的 JVM 中的调试功能。JVMTI 是调试器和分析工具使用的低级调试接口。有了它,您可以检查状态并控制执行在 JVM 中运行的应用程序。”
"The subset of JVMTI that is most typically used by profilers is always available. However, the functionality used by debuggers to be able to step through the code and set breakpoints has some overhead associated with it and is not always available. To enable this functionality you must use the -Xdebug option."
“分析器最常使用的 JVMTI 子集始终可用。但是,调试器使用的能够单步执行代码和设置断点的功能有一些与之相关的开销,并不总是可用。启用此功能您必须使用 -Xdebug 选项。”
-Xrunjdwp:transport=dt_socket,server=y,suspend=n myApp
Check out the docs on -Xrunjdwp too. You can enable it only when a certain exception is thrown for example. You can start it up suspended or running. Anyway.. I digress.
也请查看 -Xrunjdwp 上的文档。例如,您只能在抛出某个异常时启用它。您可以将其启动为暂停或运行。无论如何..我离题了。
回答by abhihello123
I found an easy way to do this -
我找到了一个简单的方法来做到这一点 -
Just enter a command like this -
只需输入这样的命令 -
>mvn -Dtest=TestClassName#methodname -Dmaven.surefire.debug test
It will start listening to 5005 port. Now just create a remote debugging in Eclipse through Debug Configurations for localhost(any host) and port 5005.
它将开始侦听 5005 端口。现在只需通过本地主机(任何主机)和端口 5005 的调试配置在 Eclipse 中创建远程调试。
Source - https://doc.nuxeo.com/display/CORG/How+to+Debug+a+Test+Run+with+Maven
来源 - https://doc.nuxeo.com/display/CORG/How+to+Debug+a+Test+Run+with+Maven
回答by Chee Loong Soon
If you don't want to be IDE dependent and want to work directly with the command line, you can use 'jdb' (Java Debugger)
如果您不想依赖 IDE 并希望直接使用命令行,则可以使用“jdb”(Java Debugger)
As mentioned by Samuel with small modification (set suspend=y instead of suspend=n, y means yes which suspends the program and not run it so you that can set breakpoints to debug it, if suspend=n means it may run the program to completion before you can even debug it)
正如 Samuel 提到的,稍作修改(设置 suspend=y 而不是 suspend=n,y 表示是挂起程序而不运行它,这样你就可以设置断点来调试它,如果 suspend=n 意味着它可以运行程序在您甚至可以调试之前完成)
On the directory which contains your pom.xml, execute:
在包含 pom.xml 的目录中,执行:
mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044 com.mycompany.app.App"
Then, open up a new terminal and execute:
然后,打开一个新终端并执行:
jdb -attach 1044
You can then use jdb to debug your program!=)
然后您可以使用 jdb 来调试您的程序!=)