最近發現 Spring Boot 本地不能 Debug 調試了,原來 Spring Boot 升級後,對應插件的命令參數都變了,故本文做一個升級。
背景:
Spring Boot 項目在使用 Spring Boot Maven 插件執行啟動命令 spring-boot:run 的時候,如果設置的斷點進不去,要進行以下的設置。
官方解決方案:
By default, the run goal runs your application in a forked process. If you need to debug it, you should add the necessary JVM arguments to enable remote debugging. The following configuration suspend the process until a debugger has joined on port 5005:
直接看怎麼做吧!
1、添加 JVM 參數
在插件 spring-boot-maven-plugin 裡面加上 jvmArguments 配置。
<code><project>
...
<build>
...
<plugins>
...
<plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
<version>2.2.0.RELEASE/<version>
<configuration>
<jvmarguments>
-Xdebug-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
/<jvmarguments>
/<configuration>
...
/<plugin>
...
/<plugins>
...
/<build>
...
/<project>
/<code>
或者在命令行指定:
<code>mvnspring-boot:run-Dspring-boot.run.jvmArguments="-Xdebug-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
/<code>
最新配置可以參考官方說明:
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html
2、添加一個Remote
在開發工具裡面新增一個 Remote 配置:
只需要確定 Host、Port 參數即可。
- Host:地址
localhost:本地啟動地址;
- Port:端口
5005:上面命令行指定的端口;
3、開始調試
先啟動加了 jvmArguments 參數的 Spring Boot 項目:
程序停在監聽端口:5005,再 debug 啟動Remote:
再回到項目,開始啟動輸出日誌,然後就可以進行斷點調試了。
這就是遠程調試了,也能幫你 debug 遠程 Spring Boot 應用,但在本地調試要操作兩次,略顯麻煩。
閱讀更多 Java領域佼佼者 的文章