How to create,build and run the SpringBoot application using command prompt?
In this blog we will discuss on how to create,build and run springboot
application with out using any IDE
.
Pre-requisites
Java 1.8+
Maven 3.5+
Below are the steps to follow
- Creating the
pom.xml
file and adding the classpath dependencies insidepom.xml
file. - Writing the required code part.
- Running the Example code.
Creating the pom.xml
file
- A Maven
pom.xml
file is used to build the project. - Here we create a
pom.xml
file and add the requireddependencies
. -
Below is an example
pom.xml
file havingspring-boot-starter-web
dependency.<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Writing the required code part
- To complete our application we need to create a
Java
file. - Maven compiles sources from
src/main/java
. -
so add the below java code inside
src/main/java
path.import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration class MyApplication{ @RequestMapping("/") String example(){ return "My First SpringBoot Application"; } public static void main(String args[]){ SpringApplication.run(MyApplication.class,args); } }
In the above code we have three
annotations
. - @RestController
- @EnableAutoConfiguration
- @RequestMapping
@RestController It is used to handle the web requests.
@EnableAutoConfiguration Configures the application based on the jar dependencies
@RequestMappingEnsures HTTP request with /
path should be mapped to example()
method.
Running the Example code.
Now go back to the path where pom.xml
file is present and execute the below command in command prompt
.
mvn spring-boot:run
Now open http://localhost:8080/