VSCode开发Java+Spring,参考https://zhuanlan.zhihu.com/p/54358113
前提
- 确认本机已安装 JAVA SDK
- 确认本机已安装Maven
- 确认本机已安装Tomcat
比如我的环境
C:\Program Files\Java\jdk1.8.0_211
C:\Program Files\Java\jre1.8.0_211
D:\Maven\apache-maven-3.6.1
D:\apache-tomcat-9.0.20
win 7, 64bit
安装JDK,JRE
参考https://www.cnblogs.com/liuhongfeng/p/4177568.html
我配置的环境变量
JAVA_HOME
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
{
"editor.accessibilitySupport": "off",
"java.home": "C:/Program Files/Java/jdk1.8.0_211",
"maven.executable.path":"D:/Maven/apache-maven-3.6.1/bin/mvn.cmd",
"java.configuration.maven.userSettings":"D:/Maven/apache-maven-3.6.1/conf/settings.xml",
"maven.terminal.useJavaHome": true,
"maven.terminal.customEnv": [
{
"environmentVariable": "MAVEN_OPTS", // variable name
"value": "-Xms1024m -Xmx4096m" // value
},
{
"environmentVariable": "JAVA_HOME", // variable name
"value": "C:/Program Files/Java/jdk1.8.0_211" // value
}
],
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue"
}
参考:https://marketplace.visualstudio.com/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
遇到的问题
错误内容
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.apache.org/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.apache.org/maven2): connect timed out and 'parent.relativePath' points at no local POMJava(0)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</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.visualstudio.com/items?itemName=redhat.java
错误内容2
The project 'demo' is not a valid java project.
错误: 找不到或无法加载主类
Maven可能需要proxy
如果在公司内部,通常会需要proxy
D:\Maven\apache-maven-3.6.1\conf\settings.xml
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
-->
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<!--
<username>proxyuser</username>
<password>proxypass</password>-->
<host>cn-proxy.jp.oracle.com</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</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
PS D:\Develop\demo> cmd /c "D:\Maven\apache-maven-3.6.1\bin\mvn.cmd" install -f "d:\Develop\demo\pom.xml"
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
编译详细过程:https://share.weiyun.com/5qpaK3b
VS Code有些文件没有列出
比如.classpath,.project这些文件在VS Code里为啥没有显示呢
原因在于如下设置
{
"java.configuration.updateBuildConfiguration": "interactive",
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
}
}
这些被排除了,所以不显示