3.2 KiB
3.2 KiB
Initializing project https://docs.spring.io/spring-boot/tutorial/first-application/index.html
Download maven repos from https://mvnrepository.com/
- First make sure that java and maven are installed. Gradle can be used too but maven is preferred for simplicity. check it by
java -versionandmvn -v. - Like react have
package.jsonwhich contains structure and what is to be installed so in spring boot it ispom.xml. pom stands for project object model. This is pom.xml for starter.<?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>3.3.3</version> </parent> <!-- Additional lines to be added here... --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> - Then in the root folder type
mvn packagein terminal to download related maven and spring plugins. its likenpm installof react. This will download dependencies on~/.m2/repository - Create
src/main/java/MyApplication.javaand type following// may recieve an error if not under custom package package com.myapp // these will be added when starting app for first time import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; // controller as C in MVC @RestController @SpringBootApplication public class MyApplication { // map to root directory @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { // running spring application SpringApplication.run(MyApplication.class, args); } } - Run
mvn spring-boot:runin terminal to run app. - Finally create an executeable jar to able to distribute it as it we need to package spring and maven plugins and packages into single jar file. All they need is java installed to run it . Add following after dependencies section in
pom.xml<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> - Then type
mvn packageto create the build (executable jar) in target folder. - In order to run this jar file type
java -jar target/myproject-0.0.1-SNAPSHOT.jar. In order to peek in this jar file usejar tvf target/myproject-0.0.1-SNAPSHOT.jar
Alternatively u can go to https://start.spring.io/ and generate your starter project too here is one example with web dependency configured spring web demo.zip