Java 如何使用 gradle 调试 spring 应用程序

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

how to debug spring application with gradle

javaeclipsespringdebugginggradle

提问by rigby

I am working on spring app and need to step through a controller method to see how it works. I am working in eclipse and building my app with gradle bootRuncommand.

我正在开发 spring 应用程序,需要逐步执行控制器方法以查看它是如何工作的。我正在 eclipse 中工作并使用gradle bootRun命令构建我的应用程序。

How to run it in debug mode?

如何在调试模式下运行它?

i tried gradle bootRun --debugbut it's just debug log, not debug mode

我试过了,gradle bootRun --debug但它只是调试日志,而不是调试模式

i tried gradle help --task bootRun-- this gives me info about task there i saw --debug-jvmoption but when i run gradle bootRun --debug-jvmapplication doesn't start

我试过了gradle help --task bootRun——这给了我关于任务的信息,我看到了--debug-jvm选项但是当我运行 gradle bootRun --debug-jvm应用程序时没有启动

回答by dankirkd

After you run gradle bootRun --debug-jvmthe application is suspended until you connect your debugger to the port it is listening on (port 5005).

运行后gradle bootRun --debug-jvm,应用程序将挂起,直到您将调试器连接到它正在侦听的端口(端口 5005)。

回答by rexford

As a response to dankdirkd's answer above: (compare)

作为对上述 dankdirkd 回答的回应:(比较

gradle bootRun --debug-jvm

will make the gradle build run in debug mode. That probably is not what you want. What you want to achieve is that the springBoot task starts your application in debug mode.

将使 gradle 构建在调试模式下运行。这可能不是你想要的。您想要实现的是 springBoot 任务以调试模式启动您的应用程序。

The spring boot task extends the gradle JavaExectask. You can configure the bootRun task in your build.gradle file to add a debug configuration like this:

spring boot 任务扩展了 gradle JavaExec任务。您可以在 build.gradle 文件中配置 bootRun 任务以添加如下调试配置:

bootRun {
  jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32323"]
}

For the build.gradle.kts this would look like this (with suspend mode disabled):

对于 build.gradle.kts,它看起来像这样(禁用挂起模式):

tasks {
    val bootRun by getting(BootRun::class) {
        jvmArgs=listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=32323")
    }
}

If your server is within a cloud and you want to debug from local machine, you need to make sure that it allows connections from outside. Use below configuration in that case

如果您的服务器在云中并且您想从本地计算机进行调试,则需要确保它允许来自外部的连接。在这种情况下使用以下配置

tasks {
val bootRun by getting(BootRun::class) {
    jvmArgs=listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
    }
}

Note that the address is now 0.0.0.0:portinstead of just port

请注意,地址现在0.0.0.0:portport

回答by jsci

Define an executes a Java application in a child process.

定义一个在子进程中执行 Java 应用程序。

task executeApp() {
    doFirst {
       println "Executing java app from Gradle..."
       javaexec {
           main = "com.mymain"
           jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=30000"]
       }
    }
}

Set your breakpoints in the java code. After execute the Gradle task.For example in Windows:

在 java 代码中设置断点。执行 Gradle 任务后。例如在 Windows 中:

  .\gradlew.bat executeApp

The task waits until you attach the debugger. For example in Netbeans go to Debug->Attach debugger , set 30000 on port Field.

该任务会一直等到您附加调试器。例如,在 Netbeans 中,转到 Debug->Attach debugger ,在端口字段上设置 30000。

回答by Himadri

For build.gradle.ktsfile you can also simply use below:

对于build.gradle.kts文件,您也可以简单地使用以下内容:

tasks.withType<BootRun> {
    jvmArgs = listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
}

回答by Crt

I personally prefer going under Gradle tasksand right-clicking on the bootRun. This is useful in the IDE compared to the terminal.

我个人更喜欢下Gradle tasksright-clicking on the bootRun。与终端相比,这在 IDE 中很有用。