DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Extending Swagger and Springdoc Open API
  • OpenAPI 3 Documentation With Spring Boot
  • How to Debug the Spring Boot Application in Eclipse? [Video]
  • How to Import the Gradle Spring Boot Application in Eclipse? | Spring Boot Tutorial [Video]

Trending

  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Metrics at a Glance for Production Clusters
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Coding
  3. Frameworks
  4. Creating a Spring Boot Project With Eclipse and Maven

Creating a Spring Boot Project With Eclipse and Maven

In this article, we look at three options for creating Spring Boot projects with Maven and Eclipse: using Spring Initializr, using the STS plugin, and doing it manually.

By 
Ranga Karanam user avatar
Ranga Karanam
DZone Core CORE ·
Updated May. 18, 22 · Tutorial
Likes (19)
Comment
Save
Tweet
Share
482.3K Views

Join the DZone community and get the full member experience.

Join For Free

How to Create a Spring Boot Project With Eclipse and Maven:

  1. Naming a Project- We give a name to the project.
  2. Declaring Dependencies -Dependencies are frameworks that you need in order to develop your project
  3. Creating Spring Boot Projects with Eclipse and Maven- There are three options to create Spring Boot Projects with Eclipse and Maven:

Option 1. Bootstrap Spring Boot Project with Spring Initializr. We will use Spring Web MVC as our web framework. 

Option 2.  Use STS or STS Eclipse Plugin to Create Spring Boot Maven Project. Download the complete installation of STS or you can install the STS Eclipse plugin.

Option 3. Manually Create a Maven Spring Boot Project. Create the project manually.

Setting up a basic project with Spring Boot is a cake walk. In this article, we will explore the different options of creating Spring Boot projects with Maven and Eclipse. You will learn:

  • How to bootstrap a simple project with Spring Initializr.
  • How to use the Spring Starter Eclipse Plugin to create a simple project with Spring Boot, Maven, and Eclipse.
  • How to create a Spring Boot project manually, step-by-step.

Video References

  • Spring
  • Spring MVC
  • Spring Boot
  • Eclipse
  • Maven
  • JUnit 
  • Mockito 

Tools You Will Need

  • Maven 3.0+ is your build tool
  • Your favorite IDE (we use Eclipse in this article)
  • JDK 1.8+

Introduction to Maven

Defining what Maven does is very difficult. To help explain, let's consider some of the things that a developer does every day...

  • Manages dependencies
    • Web layer (Spring MVC)
    • Data layer (JPA, Hibernate)
  • Build a JAR or a WAR or an EAR
  • Run the application locally
  • Deploy to a T environment
  • Add new dependencies to a project
  • Run unit tests
  • Generate projects
  • Create Eclipse Workspace

Maven helps us do all of this and more!

Naming a Project

How can other projects use our project? By using our project groupId and artifactId. We give a name to the project in the pom.xml, as shown below:

    <groupId>com.in28minutes.learning.maven</groupId>
    <artifactId>maven-in-few-steps</artifactId>

Declaring Dependencies

Dependencies are frameworks that you need in order to develop your project. In the example below, we add two 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>

Maven Build Lifecycle

When we run mvn clean install, we are executing the complete Maven build lifecycle. This lifecycle is a sequence of the following steps:

  • Validate
  • Compile
  • Test
  • Package
  • Integration test
  • Verify
  • Install
  • Deploy

Maven follows convention over configuration.

The predefined folder structure looks like this:

  • Source code
    • ${basedir}/src/main/java
    • ${basedir}/src/main/resources 
  • Test code

How Does Maven Work?

A Maven repository contains all the JARs indexed by artifact ID and group ID. Once we add a dependency to our pom.xml, Maven asks the Maven repository for the JAR dependencies, giving group ID and the artifact ID as the input.

The JAR dependencies are stored on your machine in a folder called maven local repository. All our projects refer to the JARs from here.

Note: A local repository is a temp folder on your machine where Maven stores the JAR and dependency files that are downloaded from the Maven repository.

Important Maven Commands

  • mvn -version: Finds the Maven version
  • mvn compile: Compiles source files
  • mvn test-compile: Compiles test files as well as source files
  • mvn clean: Deletes target directory
  • mvn test:  Runs unit tests
  • mvn package: Creates a JAR for the project
  • help:effective-settings: Debugs Maven settings
  • help:effective-pom: Look at the complete pom after all inheritances from parent poms are resolved
  • dependency:tree: Look sat all the dependencies and transitive dependencies
  • dependency:sources: Downloads source code for all dependencies
  • -debug: Debug flag; can be used with all the above commands

Creating Spring Boot Projects With Eclipse and Maven

There are three options to create Spring Boot Projects with Eclipse and Maven

  1. Spring Initializr
  2. Use STS or STS Eclipse Plugin and create a Spring Boot Maven project directly from Eclipse
  3. Manually create a Maven project and add Spring Boot starter dependencies

We will use a Spring Boot Starter Web as an example.

Option 1: Bootstrap Spring Boot Project With Spring Initializr

Creating a web application with Spring Initializr is a cake walk. We will use Spring Web MVC as our web framework. Spring Initializr is a great tool to bootstrap your Spring Boot projects.

As shown in the image above, following steps have to be done:

  • Launch Spring Initializr and choose the following
    • Choose com.in28minutes.springboot as Group
    • Choose student-services as Artifact
    • Choose from the following dependencies:
      • Web
      • Actuator
      • DevTools
  • Click Generate Project.

This would download a ZIP file to your local machine. Unzip the zip file and extract to a folder. In Eclipse, Click File > Import > Existing Maven Project as shown below.

Navigate or type in the path of the folder where you extracted the ZIP file on the next screen.

Once you click Finish, Maven will take some time to download all the dependencies and initialize the project.

That's it. Your first Spring project is ready!

Follow these links to understand more about the project that we created:

  • Spring Boot vs. Spring vs. Spring MVC

  • Auto Configuration

  • Spring Boot Starter Projects

  • Spring Boot Starter Parent

  • Spring Boot Initializr

Option 2: Use STS or STS Eclipse Plugin to Create Spring Boot Maven Project

With the Spring tool suite, you can directly create a Spring Boot project from Eclipse. You should either download the complete installation of STS or you can install the STS Eclipse plugin. This link provides the complete download of STS as well as the update sites for the STS Eclipse Plugin.

In Eclipse/STS, start with File > New > Spring Starter Project. In the next screen, you can choose the following for your project:

  • Group ID

  • Artifact ID

  • Root package

  • Version

  • Description

  • Java version

  • Language 

  • Packaging

Make sure you choose Maven as Type.

In the next screen, you can choose the dependencies that you want to add to your Spring Boot project. 

Once you click Finish, Maven will take some time to download all the dependencies and initialize the project.

Now your Spring project is ready!

Option 3: Manually Create a Maven Spring Boot Project

The last option is to create the project manually.

In Eclipse, start with File > New > MavenProject.

Choose Create a simple project, as shown in the screenshot below: 

In the next screen, provide these details for your project and click Finish.

  • Group ID

  • Artifact ID

  • Version

This would create a basic Maven project with zero dependencies. Next, add in the appropriate Spring Boot starters into the pom.xml: 

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

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

Starter Web is used for developing Spring Boot Web applications or RESTful services. Starter Test provides unit testing and integration test capabilities with Spring Test, Mockito, and JUnit.

One this we are missing is the version for these dependencies. We will add a Spring Boot starter parent as the parent pom in the pom.xml:

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

Let's configure the Java version to use 1.8:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

The next step is to create a Spring Boot application class that will be the launching point of the web application.

/src/main/java/com/in28minutes/springboot/tutorial/SpringBootWebApplication.java:

package com.in28minutes.springboot.tutorial;

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

@SpringBootApplication
public class SpringBootWebApplication {

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

All you need to do is add @SpringBootApplication and use the SpringApplication.run() static method to launch the Spring application context.

When you launch this class as a Java application, you will see that an embedded Tomcat server launches and you are ready to add features to this application.

Summary

In this article, we looked at the different options for creating Spring Boot projects with Maven and Eclipse. I love Option 2: creating the project directly from Eclipse using the STS plugin. But, you might have your own preference.

Spring Framework Spring Boot Apache Maven Eclipse

Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Extending Swagger and Springdoc Open API
  • OpenAPI 3 Documentation With Spring Boot
  • How to Debug the Spring Boot Application in Eclipse? [Video]
  • How to Import the Gradle Spring Boot Application in Eclipse? | Spring Boot Tutorial [Video]

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!