6

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"
            ]
        }
    ]
}
Sean
  • 115
  • 1
  • 2
  • 10

3 Answers3

6

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.

Claudiomir
  • 109
  • 1
  • 7
0

It's worth taking a look at https://github.com/badsyntax/vscode-gradle which will make this easier for you. You can debug your app, and restart the debugging in one step after making code changes.

badsyntax
  • 9,394
  • 3
  • 49
  • 67
0

I just right click on the main Spring Boot Application class (ie. as annotated with @SpringBootApplication) and click "Debug". That will spin up the server and allow you to halt at breakpoints. Seems to work OK for me.

(I have the Microsoft Java Extension Pack Plug-in installed)

Ben
  • 1,537
  • 1
  • 10
  • 6