5

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

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

./gradlew bootRun -Dspring.profiles.active=local

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

启动.json

{
    "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
        }
    ]
}

任务.json

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

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

No active profile set, falling back to default profiles: default

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

编辑1:

所以如果我运行这个任务

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

然后运行这个启动配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "task 2",
            "request": "attach",
            "hostName": "localhost",
            "port": 5005
        }
    ]
}

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

编辑2:

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

编辑 3:

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

启动.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug",
            "request": "attach",
            "hostName": "localhost",
            "port": 5005
        }
    ]
}

任务.json

{
    "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"
            ]
        }
    ]
}
4

3 回答 3

6

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

{
    "gradle.javaDebug": {
        "tasks": [
            "bootRun"
        ],
        "clean": true
    }
}

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

在此处输入图像描述

您需要安装Gradle 任务Java 调试器和Java扩展语言支持。

于 2021-02-21T20:02:16.433 回答
0

值得一看https://github.com/badsyntax/vscode-gradle这将使您更轻松。您可以调试您的应用程序,并在更改代码后一步重新开始调试。

于 2020-06-15T16:08:48.860 回答
0

我只需右键单击主 Spring Boot 应用程序类(即使用 @SpringBootApplication 注释),然后单击“调试”。这将启动服务器并允许您在断点处停止。对我来说似乎工作正常。

(我安装了 Microsoft Java Extension Pack 插件)

于 2020-09-18T01:38:54.397 回答