如何使用VScode调试bootRun

时间:2019-09-30 21:41:05

标签: spring-boot debugging gradle visual-studio-code bootrun

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

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

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

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

Launch.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
        }
    ]
}

Tasks.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 Configuration

编辑3:

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

Launch.json

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

Tasks.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 个答案:

答案 0 :(得分:2)

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

{
  "John": ["age" : "25"]
}

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

enter image description here

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

答案 1 :(得分:0)

感谢分享。我所做的和你所做的相似。唯一的问题是我们需要执行两次,一次是在调试模式下启动应用程序,另一次是将调试器连接到应用程序。我担心的是,每次更改Java代码时,我们都必须停止所有进程,然后执行这两个步骤。对我来说,这似乎很琐事。您是否发现执行这两个步骤的任何方法?1执行命令/按钮单击。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我只需右键单击主Spring Boot Application类(即@SpringBootApplication注释),然后单击“ Debug”。这将启动服务器,并允许您在断点处暂停。似乎对我来说还可以。

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