Vishal's Blog
Header

17 - Maven & Gradle Basics for Spring Boot

Build tools are an essential part of modern Java development.
In Spring Boot projects, Maven and Gradle handle dependency management, project structure, building, testing, and packaging applications into runnable artifacts.

This post explains how these tools work and how Spring Boot uses them behind the scenes.


1. What Is a Build Tool?

A build tool automates tasks such as:

  • Downloading dependencies
  • Compiling source code
  • Running tests
  • Packaging applications (JAR/WAR)
  • Managing project structure

Without a build tool, managing libraries and builds manually would be error-prone and inefficient.


2. Why Spring Boot Depends on Build Tools

Spring Boot projects rely on build tools to:

  • Resolve starter dependencies
  • Manage transitive dependencies
  • Package applications as executable JARs
  • Run applications via simple commands
  • Integrate testing and plugins

Spring Boot officially supports Maven and Gradle.


3. Maven Basics

Maven is the most widely used build tool in the Java ecosystem.

Key Concepts:

  • Convention over configuration
  • Declarative configuration
  • Centralized dependency management

3.1 The pom.xml File

pom.xml (Project Object Model) is the heart of a Maven project.

Example:

<project>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

3.2 Maven Project Structure

src/
 ├── main/
 │   ├── java/
 │   └── resources/
 └── test/
     └── java/

Maven enforces this structure automatically.


3.3 Common Maven Commands

mvn clean
mvn compile
mvn test
mvn package
mvn spring-boot:run

These commands cover most development needs.


4. Gradle Basics

Gradle is a modern, flexible build tool gaining popularity.

Key Characteristics:

  • Faster builds
  • Incremental compilation
  • Groovy or Kotlin-based DSL
  • Highly customizable

4.1 build.gradle File

Example (Groovy DSL):

plugins {
    id 'org.springframework.boot' version '3.x.x'
    id 'java'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

4.2 Gradle Project Structure

Gradle follows the same standard structure as Maven, so switching is easy.


4.3 Common Gradle Commands

gradle build
gradle test
gradle bootRun

Or using wrapper:

./gradlew build

5. Maven vs Gradle (Comparison)

Feature Maven Gradle
Configuration XML DSL (Groovy/Kotlin)
Learning Curve Easy Moderate
Build Speed Slower Faster
Flexibility Limited High
Popularity Very high Growing

Spring Boot supports both equally well.


6. Spring Boot Starters & Dependency Management

Spring Boot starters simplify dependency management.

Example:

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

Starters:

  • Pull compatible versions automatically
  • Reduce version conflicts
  • Keep configuration minimal

7. Packaging Spring Boot Applications

Spring Boot applications are packaged as executable JARs by default.

Maven command:

mvn package

Gradle command:

gradle build

Result:

target/app.jar

Run using:

java -jar app.jar

8. Maven Wrapper & Gradle Wrapper

Wrappers ensure consistent builds across environments.

  • mvnw
  • gradlew

Always commit wrapper files to version control.


9. Best Practices

✔ Use Maven for simpler projects

✔ Use Gradle for large, complex builds

✔ Rely on Spring Boot starters

✔ Avoid overriding dependency versions unnecessarily

✔ Commit wrapper scripts


10. Summary

  • Maven and Gradle automate builds and dependency management.
  • Spring Boot integrates deeply with both tools.
  • Starters simplify dependency selection.
  • Executable JARs make deployment easy.
  • Understanding build tools is essential for real-world Spring Boot development.

What's Next?

Next post:

18 - Spring Boot Architecture Overview

We’ll explore how Spring Boot is structured internally and how its core components work together.