配置Windows下vscode的Java/Maven开发环境
1 安装环境
1.1 安装VSCode
1.2 安装Chocolate
以管理员身份运行Powershell, 执行以下命令安装Chocolate:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
1.3 安装AdoptOpenJDK
- 打开官网手动下载,手动下载的目的是解决Coding Pack for Java 下载过慢
- 选择OpenJDK11 和 HotSpot
- 下载并安装
1.4 安装maven
管理员权限运行CMD 或者 PowerShell ,执行
choco install maven -y
1.5 安装Coding Pack for Java
- 下载Coding Pack for Java
- 点击Next
- 点击Install
- 安装完成
- 此时Java Extension Pack已经安装好
2 Maven工程
2.1 添加settings.json配置
添加.vscode目录,并添加settings.json文件
在文件中添加以下内容
{
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[java]": {
"editor.defaultFormatter": "redhat.java"
},
"editor.defaultFormatter": null,
"editor.linkedEditing": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.formatOnType": true,
"java.configuration.updateBuildConfiguration": "automatic",
"java.codeGeneration.hashCodeEquals.useInstanceof": true,
"java.codeGeneration.hashCodeEquals.useJava7Objects": true,
"java.codeGeneration.toString.codeStyle": "STRING_CONCATENATION",
"java.codeGeneration.toString.limitElements": 1,
"java.codeGeneration.toString.listArrayContents": true,
"java.codeGeneration.generateComments": true,
"java.codeGeneration.toString.skipNullValues": false,
"java.codeGeneration.toString.template": "${object.className} [${member.name()}=${member.value}, ${otherMembers}]",
"java.codeGeneration.useBlocks": false,
"java.completion.enabled": true,
"java.completion.favoriteStaticMembers": [
"org.junit.Assert.*",
"org.junit.Assume.*",
"org.junit.jupiter.api.Assertions.*",
"org.junit.jupiter.api.Assumptions.*",
"org.junit.jupiter.api.DynamicContainer.*",
"org.junit.jupiter.api.DynamicTest.*",
"org.mockito.Mockito.*",
"org.mockito.ArgumentMatchers.*",
"org.mockito.Answers.*"
],
"java.completion.filteredTypes": [
"java.awt.*",
"com.sun.*",
"sun.*"
],
"java.completion.guessMethodArguments": true,
"java.completion.importOrder": [
"java",
"javax",
"com",
"org"
],
"java.completion.maxResults": 0,
"java.completion.overwrite": true,
"java.configuration.checkProjectSettingsExclusions": true,
"java.configuration.maven.globalSettings": "",
"java.configuration.maven.userSettings": "",
"java.contentProvider.preferred": "",
"java.eclipse.downloadSources": true,
"java.errors.incompleteClasspath.severity": "warning",
"java.foldingRange.enabled": true,
"java.format.comments.enabled": true,
"java.format.enabled": true,
"java.format.onType.enabled": true,
"java.format.settings.profile": "GoogleStyle",
"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
"java.home": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.10.9-hotspot",
"java.implementationsCodeLens.enabled": true,
"java.import.exclusions": [
"**/node_modules/**",
"**/.metadata/**",
"**/archetype-resources/**",
"**/META-INF/maven/**"
],
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m",
"java.maven.downloadSources": true,
"java.maven.updateSnapshots": true,
"java.maxConcurrentBuilds": 1,
"java.progressReports.enabled": true,
"java.project.outputPath": "",
"java.project.referencedLibraries": [
"lib/**/*.jar"
],
"java.project.resourceFilters": [
"node_modules",
".git"
],
"java.recommendations.dependency.analytics.show": true,
"java.refactor.renameFromFileExplorer": "prompt",
"java.references.includeAccessors": true,
"java.referencesCodeLens.enabled": true,
"java.saveActions.organizeImports": true,
"java.selectionRange.enabled": true,
"java.semanticHighlighting.enabled": true,
"java.server.launchMode": "Hybrid",
"java.showBuildStatusOnStart.enabled": true,
"java.signatureHelp.enabled": true,
"java.sources.organizeImports.starThreshold": 99,
"java.templates.fileHeader": [],
"java.templates.typeComment": [],
"java.trace.server": "off",
"maven.excludedFolders": [
"**/.*",
"**/node_modules",
"**/target",
"**/bin",
"**/archetype-resources"
],
"maven.executable.options": "",
"maven.executable.preferMavenWrapper": true,
"maven.pomfile.autoUpdateEffectivePOM": true,
"maven.pomfile.globPattern": "**/pom.xml",
"maven.showInExplorerContextMenu": true,
"maven.terminal.customEnv": [],
"maven.terminal.favorites": [],
"maven.terminal.useJavaHome": true,
"maven.view": "hierarchical",
}
2.1 从模板创建Maven工程
按如下顺序点击
- 创建Java工程
- 从archetype创建Maven
- 选择maven-archetype-quickstart
- 选1.4
- 选择一个目录,比如这里选择java
- 等待一堆下载后 出现如下
- 如果网络不佳,会一直卡在Generating project in Interactive mode
可以选择添加参数-DarchetypeCatalog=internal 手动执行
mvn org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate -DarchetypeArtifactId="maven-archetype-quickstart" -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeVersion="1.4" -DarchetypeCatalog=internal
- 运行成功
2.2 清除language server
如果代码中出现了以下错误提示
The declared package "com.aura" does not match the expected package "app" Java(536871240)
则是Language Server的缓存目录不干净
按照以下操作进行处理:
- 按F1 打开命令框
- 选择Java: Clean the Java language server workspace
- 点击 Restart and delete
2.3 调试/启动
- 打开App.java, 点击F5 启动调试
- 此时会弹出提示,不要安装Dependency Analytic,这个依赖检查太严格会对log4j等依赖报红色波浪线,选择Never永远不装。
- 另一个提示是安装vscode-xml,这个插件比较好用,建议安装。
- 等待一会,出现Hello World 运行成功
2.4 生成可直接运行的Jar包
- 打开pom.xml 添加以下配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.aura.App</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
-
右键点击 maven project test
-
点击package
-
构建成功
-
运行