在Gradle和IntelliJ 2016.3.5中调试具有断点的JVM应用程序(Java或Scala) [英] Debugging of a JVM application (Java or Scala) with breakpoints in Gradle and IntelliJ 2016.3.5

查看:261
本文介绍了在Gradle和IntelliJ 2016.3.5中调试具有断点的JVM应用程序(Java或Scala)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JVM应用程序,需要在IntelliJ 2016.3.5中使用Gradle任务(运行和测试作为依赖关系)进行调试。



有各种各样的有关如何使用Gradle和IntelliJ完成调试的信息来源:


  1. 使用IntelliJ的Debug Gradle插件

  2. 使用Intellij在gradle项目中设置断点(最有帮助的)

  3. https://youtrack.jetbrains.com/issue/IDEA-119551

  4. https://youtrack.jetbrains.com/issue/IDEA-86465

  5. htt ps://youtrack.jetbrains.com/issue/IDEA-119494

但是,这些来源是过时的或意味着另一种情况。我不想调试Gradle脚本,而是要运行实际的Java / Scala应用程序的JVM。此外,IntelliJ的最新版本使用Gradle Tooling API,它不提供关闭守护程序的选项。 JetBrains的本地支持仅通过直接运行和测试任务上的调试按钮提供,但如果它们被定义为来自另一个任务的依赖项(例如,检查)则不提供。



根据消息来源,这是要走的路:

 运行{//或测试,无关紧要
jvmArgs-agentlib:jdwp = transport = dt_socket,server = y,suspend = y,address = 5005
// xor,or both,似乎没有任何区别
调试true
}

无论如何,Gradle(或JVM)将开始在端口5005上听:





然后,我创建了一个具有以下参数的远程配置:





但是当我启动IntelliJ远程调试任务时,它失败:





我也尝试使用端口5006和suspend = n没有成功。在此之前,我尝试在IntelliJ-Gradle运行任务中使用Gradle参数。然后,它确实连接,但似乎是Gradle脚本而不是应用程序JVM,因为它没有在断点处中断。如何解决这个问题?

解决方案

同时,我自己找到了解决方案。如果您有类似的问题,请使用调试选项执行上述方法。

  test {
debug true
}

但是,请确保在IntelliJ重新启动后,设置中接受外部连接: p>





然后,它连接到正确的JVM使用远程任务在断点处中断:







但是,如果重新启动IntelliJ,启用了相同的选项(外部连接),则调试任务可能由于端口被阻止而失败:





所以,由于某种原因,IntelliJ阻止港后重新启动,但是需要启用调试任务的设置。这很奇怪,我不认为它的行为是这样的。



无论如何,如果禁用设置并重新启动,端口将再次打开。然后,重新启用设置,不要重新启动,只需运行Gradle任务和调试任务。它将工作。



我希望这有助于任何人寻找一个中间解决方案来调试JVM应用程序与Gradle和IntelliJ之间混乱和部分过时的答案。如果任何人有更好或更简单的建议,请随意附加您的答案。


I have a JVM application that I need to debug using breakpoints with a Gradle task (run and test as dependencies) within IntelliJ 2016.3.5.

There are various sources on how to accomplish debugging with Gradle and IntelliJ:

  1. Debug Gradle plugins with IntelliJ
  2. Using Intellij to set breakpoints in gradle project (most helpful one)
  3. https://youtrack.jetbrains.com/issue/IDEA-119551
  4. https://youtrack.jetbrains.com/issue/IDEA-86465
  5. https://youtrack.jetbrains.com/issue/IDEA-119494

However, these sources are either outdated or meant for another scenario. I do not want to debug the Gradle script but the JVM that runs the actual Java/Scala application. Moreover, the recent versions of IntelliJ use the Gradle Tooling API, which does not offer the option to turn off the daemon. A native support from JetBrains is only provided using the debugging button on the run and test tasks directly, but not if they are defined as dependencies from another task (e.g., check).

According to the sources, this is the way to go then:

run { // or test, doesn't matter
    jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
    // xor, or both, doesn't seem to make any difference
    debug true
}

In any way, Gradle (or the JVM) will then start to listen on port 5005:

Then, I have created a remote configuration with the following parameters:

But when I start the IntelliJ remote debugging task, it fails:

I have also tried using port 5006 and suspend=n without success. Before that, I tried using Gradle arguments in the IntelliJ-Gradle run task. Then, it indeed connected, but seemingly to the Gradle script and not the application JVM because it did not interrupt at the breakpoints. How to fix this?

解决方案

Meanwhile, I found the solution myself. If you have a similar issue, follow the approach mentioned above using the debug option.

test {
    debug true
}

But make sure that external connections are accepted in the settings after IntelliJ restarted:

Then, it connects to the correct JVM and interrupts at the breakpoints using the remote task:

If you restart IntelliJ, however, with the very same option (external connections) enabled, then the debugging task might fail due to a blocked port:

So, for some reason, IntelliJ blocks that port after a restart but it is necessary to enable the setting for the debugging task to work. That's odd and I don't think it is intended to behave like that.

Anyways, if you disable the setting and restart, the port will be open again. Then, re-enable the setting, do not restart, just run the Gradle task, and the debugging task. It will work.

I hope this helps anyone else looking for an intermediate solution to debug JVM applications with Gradle and IntelliJ among the confusing and partially outdated answers out there. If anyone has a better or simpler suggestion, feel free to append your answer.

这篇关于在Gradle和IntelliJ 2016.3.5中调试具有断点的JVM应用程序(Java或Scala)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆