Everything You Need To Know About Spring Boot Java Project Creation

In this blog, we will learn the different ways of creating a spring boot project using eclipse maven and an online spring initializer.

Let first start exploring how we can create a project using an online spring initializer.

 

First, what is a spring initializer?

 

The Spring Initializr is a web application that can generate a Spring Boot project structure for you. It doesn’t generate any application code, but it will give you a basic project structure and either a Maven or a Gradle build specification to build your code. We can then write our application code inside it.


How to create spring boot project using spring initializer

Step 1: Go to the spring initializer

spring boot project creation spring initializr

Step 2: select the programming language and project type, spring boot version.

 

I am going to select programming language as java, Project type as maven and spring boot version 2.4.4. Now specify the project metadata this metadata is used by maven to identify and build the project.

  • groupId – a unique base name of the company or group that created the project
  • artifactId and name – a unique name of the project
  • version – a version of the project
  • packaging – a packaging method (e.g. WAR/JAR/ZIP)
  • Java version - java version project going to use.
  • Package name - as folder structure organizing our project code

spring initializr java maven metadata

Step 3: Add project.

 

Dependencies is another archive or in simple terms, the other project it can be a JAR, ZIP, and so on which we are going to use in our current project in order to compile, build, test, and/or to run. 

 

We need spring web as we are going to create the rest API in our project.


spring boot maven dependencies


Step 4: now we are ready to generate the project.to preview the project structure and files click the EXPLORE (CTRL + SPACE)

 

You will see something like this. If you are okay, then click generate to create spring boot project it will start downloading (ZIP file) containing our project.


spring boot project structures

Simple is it? Now we can start writing our application code. you can find a simple spring boot Rest API example here

How to create a spring boot project using eclipse?

 

To create spring boot in eclipse we need to install an STS plugin. Go to help -> eclipse marketplace -> sts and install spring tools 4 as shown below. 

eclipse plugin for spring boot project


Step 1: Go to file-> new -> spring starter project and fill the java version, project metadata as we did in before using spring initializer

spring boot creation using eclipse

Step 2: click next now you will ask to choose our project dependencies

 
spring boot eclipse project dependencies


Click finish now we have our spring boot project created.

How to create a spring boot project manually?

 

To create a spring boot project manually we need maven and eclipse installed on our system. Maven will use to import the necessary jar file which is needed in the spring boot project

 

Open eclipse, go to File -> New > Maven Project

java maven project

Click next, enter the project metadata and click finish.

maven project creation using eclipse

Now open the pom.xml file in the project and add the necessary dependencies.

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>
   

The spring-boot-starter-web is used to create Rest API services and spring-boot-starter-test provides

unit-testing supports for spring boot application.

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.4.1</version>
	<relativePath />
	<!-- lookup parent from repository -->
</parent>
                  

Now let add properties to configure our maven project.


<properties>
	<java.version>8</java.version>
</properties>



The final pom.xml file looks like this.



<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.4</version>
		<relativePath/>
		<!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
  

Finally, let create a java class that will be the starting point of our spring boot application.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}
  

The class annotated with @SpringBootApplication will be the starting point for a spring boot project.

 

If you have any doubts on the above topic comment below.

Post a Comment

Previous Post Next Post

Recent Posts

Facebook