Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug bootRun with VScode

I'm trying to debug a Spring bootRun application via VSCode. I'm not sure what the proper launch configuration is.

This is how I launch the program in a terminal

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

These are the current configurations I've tried with no luck.

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": []
        }
    ]
}

The "Debug" configuration spits out the following error

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

The "Debug Task" configuration runs the task, but it waits until the task finishes which it never will. So I can't debug it.

EDIT 1:

So if I run this task

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

Then run this launch configuration

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

I can debug the application, but this only attaches the debugger to the process. So I have to manually kill the process when I am done debugging. Ideally I would like to start and stop the application with vscode via a launch configuration.

EDIT 2:

I can achieve what I want in IntelliJ with this configuration, but I want to be able to do this in vscode. IntelliJ Configuration

EDIT 3:

This is my current configuration which works pretty well. I can start the program with CMD-SHFT-B then F5 to start the debugger.

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"
            ]
        }
    ]
}
like image 782
Sean Avatar asked Sep 30 '19 21:09

Sean


1 Answers

You can achieve that by adding the following in .vscode/settings.json file:

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

After saving the file, next to Run Task will apear the Debug Task option in Gradle - Gradle Tasks view:

enter image description here

You need to have the Gradle Tasks, Debugger for Java and Language Support for Java extensions installed.

like image 94
Claudiomir Avatar answered Oct 25 '22 14:10

Claudiomir