Debugging a JVM application (Java or Scala) with breakpoints in Gradle and IntelliJ 2016.3.5

I have a JVM application that needs to be debugged using breakpoints with the Gradle task (run and test as dependencies) in IntelliJ 2016.3.5.

There are various sources of how to debug using Gradle and IntelliJ:

However, these sources are either outdated or intended for a different scenario. I do not want to debug the Gradle script, but the JVM that runs the Java / Scala application. Moreover, recent IntelliJ versions use the Gradle Tooling API, which does not allow you to disable the daemon. Native support for JetBrains is provided only by using the debug button at startup and test tasks directly, but not if they are defined as dependencies on another task (for example, verification).

According to sources, this is the way:

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 case, Gradle (or JVM) will start listening on port 5005:

Imgur

Then I created a remote configuration with the following parameters:

Imgur

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

Imgur

I also tried using port 5006 and suspend = n without success. Before that, I tried to use Gradle arguments in the IntelliJ-Gradle startup task. Then it really connected, but apparently to the Gradle script, and not to the application JVM, as it did not interrupt at breakpoints. How to fix it?

+2
source share
2 answers

Meanwhile, I found a solution on my own. If you have a similar problem, follow the approach described above using the debug option.

 test { debug true } 

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

Imgur

Imgur

Then it connects to the correct JVM and breaks at breakpoints using a remote task:

Imgur

Imgur

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

Imgur

So, for some reason, IntelliJ blocks this port after a restart, but you need to enable the setting for the debug task. This is strange, and I don’t think it should behave like this.

In any case, if you disable the setting and reboot, the port will open again. Then enable the option again, do not reboot, just run the Gradle task and the debug task. He will work.

I hope this helps anyone looking for an intermediate solution for debugging JVM applications using Gradle and IntelliJ among the confusing and partially obsolete answers. If anyone has a better or simpler suggestion, feel free to add your answer.

0
source

Debugging gradle tasks such as "test", "run", in fact, all gradle tasks that implement the JavaForkOptions interface should work in IntelliJ since 2014

+1
source

Source: https://habr.com/ru/post/947603/


All Articles