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 2: select the programming language and project type, spring boot
version.
- 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
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.
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.
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?
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
<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
<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.