joplin-notes/joplin notion extension/Web Development/Java/Spring/Initializing project.md
2026-11-04 00:33:23 +05:30

3.2 KiB

Initializing project https://docs.spring.io/spring-boot/tutorial/first-application/index.html

Download maven repos from https://mvnrepository.com/

  1. First make sure that java and maven are installed. Gradle can be used too but maven is preferred for simplicity. check it by java -version and mvn -v.
  2. Like react have package.json which contains structure and what is to be installed so in spring boot it is pom.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>
    
    
  3. Then in the root folder type mvn package in terminal to download related maven and spring plugins. its like npm install of react. This will download dependencies on ~/.m2/repository
  4. Create src/main/java/MyApplication.java and 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);
    	}
    
    }
    
  5. Run mvn spring-boot:run in terminal to run app.
  6. 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>
    
  7. Then type mvn package to create the build (executable jar) in target folder.
  8. 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 use jar 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