使用vscode快速构建spring boot框架

spring boot

spring boot 是基于spring 开发的脚手架,它可以快速配置spring,简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

vscode

vscode是微软推出的基于electronic 开发的代码编辑器,可以开发多种语言。

安装插件

vscode的强大之处在于它的插件机制,通过安装插件,我们可以快速开发相关语言的应用,有了这些插件,vscode的堪比ide.

开发spring boot我们需要安装两个插件包

  • Java Extension Pack

Language Support for Java(TM) by Red Hat Java Linting, Intellisense, formatting, refactoring, Maven/Gradle support and more... Red Hat

Debugger for Java A lightweight Java debugger for Visual Studio Code Microsoft

Java Test Runner Run and debug JUnit or TestNG test cases Microsoft

Maven for Java Manage Maven projects, execute goals, generate project from archetype, improve user experience for Java developers. Microsoft

Java Dependency Viewer Manage Java Dependencies in VSCode Microsoft

Visual Studio IntelliCode AI-assisted development Microsoft )

  • Spring Boot Extension Extension Pack (5)

Spring Boot Tools Provides validation and content assist for Spring Boot application.properties, application.yml properties files. As well as Boot-specific support for .java files. Pivotal

Concourse CI Pipeline Editor Provides validation and content assist for Concourse CI pipeline and task configuration yml files Pivotal

Cloudfoundry Manifest YML Support Adds linting, content assist and hoverinfo's for Cloudfoundry Deployment Manifests (a.k.a. manifest.yml) files. Pivotal

Spring Initializr Java Support A lightweight extension based on Spring Initializr to generate quick start Spring Boot Java projects. Microsoft

Spring Boot Dashboard Spring Boot Dashboard for VS Code Microsoft

也就是通过安装两个扩展包,我们安装了11个扩展。

maven

我们使用maven来构建我们的spring项目,所以我们需要先安装maven. https://maven.apache.org/ 可以在maven官网找到对应操作系统的版本,安装非常简单,需要注意的是添加环境变量。

添加环境变量就是将maven的执行目录放入到系统环境中,这样系统在任何目录下都可以执行maven的命令。

maven 网路问题

由于国内网络问题,我们需要配置下maven的仓库,在maven的安装目录里面有个conf文件夹,里面有settings.xml,我们修改仓库位置。

<mirrors>
   <mirror>
        <id>UK</id>
        <name>UK Central</name>
        <url>http://uk.maven.org/maven2</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
    <!-- 中央仓库1 -->
    <mirror>
        <id>repo1</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo1.maven.org/maven2/</url>
    </mirror>

    <!-- 中央仓库2 -->
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</url>
    </mirror>

  </mirrors>

配置vscode的maven变量

找到vscode的个人变量配置文件,添加下面的配置。

"maven.excludedFolders": [
        "**/.*",
        "**/node_modules",
        "**/target",
        "**/bin"
    ], 
    "java.home":"D:\Program Files\Java\jdk1.8.0_181",jdk目录
    "java.configuration.maven.userSettings": "D:\apache-maven-3.6.3\conf\settings.xml",//maven配置文件目录
    "maven.executable.path": "D:\apache-maven-3.6.3\bin\mvn.cmd",//maven执行命令
    "maven.terminal.useJavaHome": true,
    "maven.terminal.customEnv": [
        {
            "environmentVariable": "JAVA_HOME",
            "value": "D:\Program Files\Java\jdk1.8.0_181"//jdk目录
        }
    ],

通过ctrl+shift+p启动命令

输入srping initializr创建spring boot项目

  • 选择语言
  • 输入包名
  • 输入项目名
  • 选择依赖

通过spring boot dashboard启动项目

在vscode左侧的文件栏中找到 spring boot dashborad,可以快速启动项目。