Windows 10下利用VSCode进行Spring Boot开发
最近工作需求,需要在Windows 10开发Spring Boot应用,不过由于Intellij收费的缘故,转而使用VSCode,经过一系列的配置,终于让Spring Boot程序成功跑起来了(基于Gradle和Maven)。下面简要描述一些需要注意的点。
VSCode Extension的安装
在VSCode里面,至少要安装:
- Debugger for Java (Microsoft)
- Gradle for Java (Microsoft)
- Language Support for Java(TM) by Red Hat (Red Hat)
- Spring Initializr Java Support (Microsoft)
- Test Runner for Java (Microsoft)
- Maven for Java (Microsoft)
其他插件例如Lombok,Spring Dashboard等可以自行选择安装
VSCode settings.json内容的修改
注意,VSCode要求Java版本至少要高于JDK11,如果必须要用JDK8的话,那么至少要下载JDK8和一个高于JDK10的JDK,为了方便,标记为JDK1x。然后,增加如下配置:
"java.home": "JDK1x的路径"
"java.configuration.runtimes": [{"name": "JavaSE-1.8", "path": "JDK8的路径", "default": true}, {"name": "JavaSE-1x", "path": "JDK1x的路径"}]
"java.import.gradle.home": "Gradle的安装路径"
"java.import.gradle.user.home": "Gradle下载的依赖的存放路径"
"java.import.gradle.java.home": "Gradle的JDK路径,这里填入JDK8"
"java.import.gradle.wrapper.enabled": false
"maven.executable.path: "D:\\maven\\path\\bin\\mvn.cmd""
"maven.executable.preferMavenWrapper: false"
"maven.pomfile.autoUpdateEffectivePOM: true
"maven.view: "flat"
"maven.settingsFile: "D:\\maven\\path\\conf\\settings.xml"
"java.configuration.maven.userSettings: "D:\\maven\\conf\\settings.xml"
VSCode launch.json内容的修改
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Project",
"request": "launch",
"mainClass": "com.xxx.xxx.XXXApplication",
"args": "--spring.profiles.active=dev"
}
]
}
Gradle配置文件
在build.gradle里面有几点需要注意的:
-
Windows下路径字符数目过多的问题,这需要安装一个gradle-util-plugins-0.1.0-RELEASE.jar的插件:
a. 下载这个插件,然后随便放到一个目录下
b. 在buildscript里面加入下面内容:
dependencies { classpath files('gradle-util-plugins-0.1.0-RELEASE.jar的路径') }
c. 如果没有应用过application这个插件的,需要加上apply plugin: "application"
d. 在project里面加上apply plugin: "org.springframework.boot"
- bootRun {
systemProperty "spring.profiles.active", "dev"
mainClassName = "你的main class的完整名称,包括包名"
} apply plugin: "com.github.ManifestClasspath"
e. 如果需要jacoco和sonar来做代码质量管理,那么需要配置如下内容到project里面,如果需要本地显示某行代码是否被覆盖可以考虑converage gutters这个插件:
apply plugin: "jacoco" jacoco { toolVersion = "0.8.2" } test { finalizedBy jacocoTestReport } jacocoTestReport { dependesOn test reports { xml.enabled = true csv.enabled = false html.enabled = true xml.destination file("$buildDir/jacoco/cov.xml") // 这个cov.xml的名称是为了coverage gutters服务的 html.destination file("$buildDir/jacoco/html") } } jacocoTestCoverageVerification { dependsOn jacocoTestReport violationRules { rule { limit { conuter = "LINE" value = "CONVEREDRATIO" minimum = 1.0 } } } }