Horje
How to Create and Setup Spring Boot Project in VSCode?

Visual Studio Code (often abbreviated as VS Code) is an Integrated Development Environment (IDE) renowned for its robust support across various programming languages, including procedural, object-oriented, and hybrid languages like C, C++, Dart, Java, Python, PHP, and more. It also supports frameworks such as Spring Boot, Flutter, etc. VS Code stands out due to its lightweight nature, sleek User Interface (UI), and intuitive User Experience (UX), making it the preferred IDE for developers globally, as per the 2023 Stack Overflow Developer Survey.

This IDE provides comprehensive one-page documentation on setting up and creating Spring Boot projects.

Prerequisites:

  • Visual Studio Code installed on the computer
  • A little understanding of Spring Boot

Steps to Create and Setup Spring Boot Project in VSCode

Below are the steps to create and setup spring boot project in VSCode.

Step 1: Preliminary Installation

Install JDK (Java Development Kit), Spring Boot Extension Pack and the Extension Pack for Java.

Step 2: Install Spring Boot Extensions

Install verified Spring Boot extensions (identified by a blue-tick) from VMWare and Microsoft. You may also install other extensions if needed as you develop more projects, but never skip installing the verified extensions first for the maximum reliability.

Install Spring Boot Extensions

Step 3: Choose the build tool

Open the Command Palette by pressing Ctrl + Shift + P. Select “Spring Initializr: Create a Maven Project” if you wish your project’s build tool to be Maven. Else, if you want it to be Gradle, choose “Spring Initializr: Create a Gradle Project“.

Choose the build tool

Step 4: Select Spring Boot Version

Choose the Spring Boot version that you want to be the project or not. It is recommended to use stable versions instead of Snapshot versions if you’re a beginner.

 Select Spring Boot Version

Step 5: Select project language

Select the programming language that you want the project’s main codebase to be in.

Select project language

Step 6: Select Group ID

Similar to a website domain name, this identifies the group/company developing the project.

Select Group ID

Step 7: Select Artifact ID

Identifies the unique name of the project within the developing group.

Select Artifact ID

Step 8: Select the Packaging Type

Packaging of Spring Boot projects are of 2 types, either Jar or War.

Select the Packaging Type

Step 9: Select Java Version

Since we have chosen Java as the project language for this project, we select the Java version to be used in the project.

Select Java Version

Step 10: Select Dependencies

Any Spring Boot project runs on various dependencies for accessing libraries from them.

 Select Dependencies

Step 11: Confirm Project Folder

Confirm a folder for the project to be generated in.

Confirm Project Folder

Step 12: Project Generation

The project is now created and auto-generated into the selected folder.

Project Generation

Successfully Project Created,

Project Created Successfully

Step 13: Set Open Folder as Workspace

Now open the selected folder to turn it into the current VS Code workspace

Set Open Folder as Workspace

Step 14: Modify application.properties

All real-world Spring Boot projects are connected with a database. The database configuration properties are set in the application.properties file under resources folder.

The below are the configuration properties when PostgreSQL dependency is chosen.

# PostgreSQL database connection settings
spring.datasource.url=jdbc:postgresql://localhost:5432/yourdatabase
spring.datasource.username=yourusername
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=org.postgresql.Driver

# Hibernate settings
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

Step 15: Project Setup Complete

Your Spring Boot project is now completely setup, for you to begin coding.

Project Folder Structure Overview:

All the below folders are auto-generated by VS Code after successfully creating the Spring Boot project by following the above 15 steps.

Basic Spring Boot Folder Structure in VS Code

Detailed Overview:

1. .mvn folder (will be .gradle if you choose Gradle as your build tool)

Expanded View of .mvn Folder
  • Contains the maven-wrapper.properties file.
  • As the name suggests, this auto-generated file contains Maven properties: wrapperVersion and distributionUrl.
  • Also has notes on the license and other relevant information.

2. .vscode folder

Expanded view of .vscode Folder

  • Contains settings.json file that lists relevant project settings in JSON format
  • One such setting is: java.compile.nullAnalysis.mode

3. src Folder

Expanded view of src Folder
  • Arguably the most important folder in the entire project structure, as this is where all coding is done
  • Contains 3 sub-folders:
    • java: Contains Java (or Kotlin/Groovy) source files.
    • resources: Holds static resources and configuration files.
    • test: Auto-generated folder for testing.
  • target: Output folder for compiled code.
  • .gitignore: Specifies files and directories to be ignored by version control.
  • HELP.md: Markdown file with project guides and documentation.
  • mvnw and mvnw.cmd: Scripts for project execution on Unix/Linux and Windows systems, respectively.
  • pom.xml: Maven configuration file listing project dependencies.

Conclusion:

After IntelliJ IDEA and Spring Tool Suite, VS Code is arguably the most popular IDE amongst Spring Boot developers today. VS Code offers the option of generating the Spring Boot project in the IDE itself, thus not requiring the developer to go to the Spring Boot official starter website for project creation, unlike the IntelliJ IDEA community edition IDE. This saves the developer’s time and energy, hence boosting their productivity, efficiency and output rate.




Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Eureka vs Consul: Service Registry Comparison Eureka vs Consul: Service Registry Comparison
Difference between Role and GrantedAuthority in Spring Security Difference between Role and GrantedAuthority in Spring Security
Maven Compiler Plugin Maven Compiler Plugin
Exclude a Dependency in a Maven Plugin Exclude a Dependency in a Maven Plugin
Maven Repositories Maven Repositories

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16