在VSCode中创建的Sringboot gradle项目,除了构建出来的build目录,资源和类文件还会被自动编译到bin目录。
上周发现在VSCode 1.95.3版本中,如果是从Spring Boot Dashboard或VSCode自带的Run and Debug工具运行项目的话,默认是从bin启动的,这样大概概率会出错。比如你的资源文件中用了资源占位符,bin文件夹中的不会被自动替换掉。我们必须使用gradle构建出来的build目录来启动。当然如果你用左边工具栏上的Gradle工具的Tasks->bootRun或bootTestRun也是没问题的。
直接上 launch.json, 关键是要配置自己的classpaths,可以根据自己的项目调整,最后一行是排除bin目录:
{
"configurations": [
{
"type": "java",
"name": "Spring Boot-MywebApplication<myweb>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "pers.binge.myweb.MywebApplication",
"projectName": "myweb",
"args": "",
"envFile": "${workspaceFolder}/.env",
"classPaths": [
"${workspaceFolder}/build/classes/java/main",
"${workspaceFolder}/build/classes/java/test",
"${workspaceFolder}/build/resources/main",
"${workspaceFolder}/build/resources/test",
"$Runtime",
"$Test",
"!${workspaceFolder}/bin"
]
}
]
}