如何使用 VScode 调试 bootRun

Sea*_*ean 5 debugging gradle spring-boot visual-studio-code bootrun

我正在尝试通过 VSCode 调试 Spring bootRun 应用程序。我不确定正确的启动配置是什么。

这就是我在终端中启动程序的方式

./gradlew bootRun -Dspring.profiles.active=local
Run Code Online (Sandbox Code Playgroud)

这些是我尝试过但没有运气的当前配置。

启动文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug",
            "args": [
                "bootRun",
                "-Dspring.profiles.active=local"
            ],
            "mainClass": "com.test.Application",
            "request": "launch"
        },
        {
            "type": "java",
            "preLaunchTask": "gradle",
            "name": "Debug Task",
            "request": "attach",
            "hostName": "localhost",
            "port": 5005
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

任务文件

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gradle",
            "type": "shell",
            "command": "./gradlew",
            "args": [
                "bootRun",
                "-Dspring.profiles.active=local",
                "--debug-jvm"
            ],
            "problemMatcher": []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

“调试”配置吐出以下错误

No active profile set, falling back to default profiles: default
Run Code Online (Sandbox Code Playgroud)

“调试任务”配置运行任务,但它会一直等到任务完成,而它永远不会完成。所以我无法调试它。

编辑 1:

所以如果我运行这个任务

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gradle",
            "type": "shell",
            "command": "./gradlew",
            "args": [
                "bootRun",
                "-Dspring.profiles.active=local",
                "--debug-jvm"
            ],
            "problemMatcher": []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后运行这个启动配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "task 2",
            "request": "attach",
            "hostName": "localhost",
            "port": 5005
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我可以调试应用程序,但这只会将调试器附加到进程中。因此,我必须在完成调试后手动终止该进程。理想情况下,我想通过启动配置使用 vscode 启动和停止应用程序。

编辑2:

我可以通过这种配置在 IntelliJ 中实现我想要的,但我希望能够在 vscode 中做到这一点。 IntelliJ 配置

编辑 3:

这是我目前的配置,效果很好。我可以用 CMD-SHFT-B 启动程序,然后按 F5 来启动调试器。

启动文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug",
            "request": "attach",
            "hostName": "localhost",
            "port": 5005
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

任务文件

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gradle",
            "type": "shell",
            "command": "./gradlew",
            "args": [
                "bootRun",
                "-Dspring.profiles.active=local",
                "--debug-jvm"
            ],
            "dependsOn": [
                "kill-java"
            ],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "kill-java",
            "type": "shell",
            "command": "pkill",
            "args": [
                "java"
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以通过在.vscode/settings.json文件中添加以下内容来实现此目的:

{
    "gradle.javaDebug": {
        "tasks": [
            "bootRun"
        ],
        "clean": true
    }
}
Run Code Online (Sandbox Code Playgroud)

保存文件后,Gradle - Gradle Tasks 视图中的Run Task旁边会出现Debug Task选项:

在此输入图像描述

您需要安装Gradle TasksJava 调试器Java 扩展语言支持


bad*_*tax 0

值得一看https://github.com/badsyntax/vscode-gradle这会让你更容易。您可以调试您的应用,并在代码更改后一步重新启动调试。