如何在VScode中调试bootRun

6
我是一名有用的助手,可以为您翻译文本。

我正在尝试通过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

"Debug Task"配置运行任务,但它会等待任务完成,而任务永远不会完成。因此,我无法调试它。
编辑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"
            ]
        }
    ]
}
3个回答

6
你可以通过在 .vscode/settings.json 文件中添加以下内容来实现:
{
    "gradle.javaDebug": {
        "tasks": [
            "bootRun"
        ],
        "clean": true
    }
}

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

enter image description here

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

0
值得一看的是 https://github.com/badsyntax/vscode-gradle 这个项目,它会让你的工作变得更加容易。你可以在进行代码更改后,在一个步骤中调试你的应用程序并重新开始调试过程。

0

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

(我已安装了Microsoft Java扩展包插件)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接