how to debug spring application with gradle

后端 未结 5 417
渐次进展
渐次进展 2020-12-23 16:43

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 bootRun co

相关标签:
5条回答
  • 2020-12-23 16:46

    For build.gradle.kts file you can also simply use below:

    tasks.withType<BootRun> {
        jvmArgs = listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
    }
    
    0 讨论(0)
  • 2020-12-23 16:47

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

    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.

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

    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):

    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:port instead of just port

    0 讨论(0)
  • 2020-12-23 17:00

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

    0 讨论(0)
  • 2020-12-23 17:11

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

    0 讨论(0)
  • 2020-12-23 17:13

    Define an executes a Java application in a child process.

    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:

      .\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.

    0 讨论(0)
提交回复
热议问题