DEV Community

Yongsik Yun
Yongsik Yun

Posted on

How to automatically reload spring boot in vscode

When developing a Java project in VSCode, it can be quite inconvenient when Spring reload does not occur after an automatic build. To address this issue, Gradle continuous is commonly used.

Terminal 1

gradle build -t
Enter fullscreen mode Exit fullscreen mode

Terminal 2

gradle bootRun
Enter fullscreen mode Exit fullscreen mode

While Terminal 1 only needs to be started once, doing it every time is inconvenient. One solution to this problem is to use the compounds feature in VSCode's launch. compounds allow multiple launch configurations to be executed as a single unit.

Solution

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Spring Boot-BaseApplication",
      "type": "java",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "mainClass": "kr.co.findthebest.Application",
      "projectName": "workspace",
      "args": "",
      "envFile": "${workspaceFolder}/.env",
      "encoding": "UTF-8"
    },
    {
      "name": "Run gradle continuous",
      "type": "node-terminal",
      "command": "gradle build --warning-mode=all -t --parallel --build-cache --configuration-cache",
      "request": "launch",
      "cwd": "${workspaceFolder}"
    }
  ],
  "compounds": [
    {
      "name": "Auto build",
      "configurations": [
        "Run gradle start",
        "Spring Boot-BaseApplication"
      ],
      "stopAll": true
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

By modifying the launch.json as above, you can create a compounds named "Auto build."

Running this compounds will execute "Run gradle continuous" and "Spring Boot-BaseApplication" sequentially.

This approach eliminates the need to start each terminal individually, streamlining the process.

Another method involves adding a task to use the preLaunchTask feature in launch. However, due to less smooth background pattern matching, this method may reduce the risk of failure in certain situations.

Conclusion

When developing a Java project in VSCode and aiming for automatic build and Spring reload, using compounds is an effective solution.

By adopting this method, you can simplify the workflow, reducing cumbersome tasks.

Related links

Top comments (0)