前提

  1. 确认本机已安装 JAVA SDK
  2. 确认本机已安装Maven
  3. 确认本机已安装Tomcat

比如我的环境

  1. C:Program FilesJavajdk1.8.0_211
  2. C:Program FilesJavajre1.8.0_211
  3. D:Mavenapache-maven-3.6.1
  4. D:apache-tomcat-9.0.20
  5. win 7, 64bit

安装JDK,JRE

参考https://www.cnblogs.com/liuhongfeng/p/4177568.html

我配置的环境变量
 

  1. JAVA_HOME
  2. CLASSPATH

安装Maven

参考https://www.cnblogs.com/liuhongfeng/p/5057827.html

安装tomcat

参考https://www.cnblogs.com/liuhongfeng/p/4177835.html

环境变量

Tomcat

JDK

Path

需要安装的插件

在 Visual Studio Code 中打开扩展视图(Ctrl+Shift+X),输入关键词java、spring分别下载Java开发插件包和springboot插件包

VS Code的设置Settings.json

  1. {
  2. 'editor.accessibilitySupport': 'off',
  3. 'java.home': 'C:/Program Files/Java/jdk1.8.0_211',
  4. 'maven.executable.path':'D:/Maven/apache-maven-3.6.1/bin/mvn.cmd',
  5. 'java.configuration.maven.userSettings':'D:/Maven/apache-maven-3.6.1/conf/settings.xml',
  6. 'maven.terminal.useJavaHome': true,
  7. 'maven.terminal.customEnv': [
  8. {
  9. 'environmentVariable': 'MAVEN_OPTS', // variable name
  10. 'value': '-Xms1024m -Xmx4096m' // value
  11. },
  12. {
  13. 'environmentVariable': 'JAVA_HOME', // variable name
  14. 'value': 'C:/Program Files/Java/jdk1.8.0_211' // value
  15. }
  16. ],
  17. 'editor.suggestSelection': 'first',
  18. 'vsintellicode.modify.editor.suggestSelection': 'automaticallyOverrodeDefaultValue'
  19. }

参考:https://marketplace./items?itemName=redhat.java

Spring Boot小项目示例

以下项目都是网页输出Hello World级别的项目

参考1:https://blog.csdn.net/smilejiasmile/article/details/89510867

参考2:https://blog.csdn.net/xiaocy66/article/details/82875770

参考3:http://www./2018/07/03/%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0%E6%90%9E%E5%AE%9A%E5%9C%A8vscode%E4%B8%AD%E8%BF%90%E8%A1%8Cspringboot%E9%A1%B9%E7%9B%AE/

遇到的问题

错误内容

Project build error: Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.1.5.RELEASE from https://repo.maven./maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.1.5.RELEASE from/to central (https://repo.maven./maven2): connect timed out and 'parent.relativePath' points at no local POMJava(0)

pom.xml

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <project xmlns='http://maven./POM/4.0.0' xmlns:xsi='http://www./2001/XMLSchema-instance'
  3. xsi:schemaLocation='http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd'>
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. VS Code开发Java笔记
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>

解决

Open your Maven pom.xml file or Gradle build.gradle file, then run VS Code command ‘Java: Update project configuration’ to force the language server to update the project configuration/classpath.

Java:Update Project configuration (Shift+Alt+U): is available when the editor is focused on a Maven pom.xml or a Gradle file. It forces project configuration / classpath updates (eg. dependency changes or Java compilation level), according to the project build descriptor.

参考1:https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md#try

参考2:https://marketplace./items?itemName=redhat.java

错误内容2

  1. The project 'demo' is not a valid java project.
  2. 错误: 找不到或无法加载主类

Maven可能需要proxy

如果在公司内部,通常会需要proxy

D:Mavenapache-maven-3.6.1confsettings.xml

  1. <proxies>
  2. <!-- proxy
  3. | Specification for one proxy, to be used in connecting to the network.
  4. |
  5. -->
  6. <proxy>
  7. <id>optional</id>
  8. <active>true</active>
  9. <protocol>http</protocol>
  10. <!--
  11. <username>proxyuser</username>
  12. <password>proxypass</password>-->
  13. <host>cn-proxy.jp.oracle.com</host>
  14. <port>80</port>
  15. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  16. </proxy>
  17. </proxies>

参考1:https://github.com/microsoft/vscode-java-debug/blob/master/Troubleshooting.md

参考2:https://blog.csdn.net/x_san3/article/details/82222907

执行Maven编译

鼠标右键,Maven Projects ->demo,选择install

  1. PS D:Developdemo> cmd /c 'D:Mavenapache-maven-3.6.1binmvn.cmd' install -f 'd:Developdemopom.xml'
  2. [INFO] Scanning for projects...
  3. [INFO]
  4. [INFO] --------------------------< com.example:demo >--------------------------
  5. [INFO] Building demo 0.0.1-SNAPSHOT
  6. [INFO] --------------------------------[ jar ]---------------------------------
  7. [INFO]
  8. [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
  9. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  10. [INFO] Copying 1 resource

编译详细过程:https://share./5qpaK3b

VS Code有些文件没有列出

比如.classpath,.project这些文件在VS Code里为啥没有显示呢

原因在于如下设置

  1. {
  2. 'java.configuration.updateBuildConfiguration': 'interactive',
  3. 'files.exclude': {
  4. '**/.classpath': true,
  5. '**/.project': true,
  6. '**/.settings': true,
  7. '**/.factorypath': true
  8. }
  9. }

这些被排除了,所以不显示

完整工程

https://share./5HMRKpg

#artContent h1{font-size:16px;font-weight: 400;}