2 Hours

4. Maven & Gradle Build Systems

Build Tools
Sandbox Note: The editor validates your Java and Spring configuration syntax only. It does NOT run Maven or Gradle commands. When you see Compilation successful (exit code 0), your code is syntactically correct. Copy it to your local IDE and run ./mvnw spring-boot:run to test.

Spring Boot applications are built with either Maven or Gradle — two powerful build automation tools. They manage dependencies, compile code, run tests, package applications, and handle deployment. Understanding your build tool is essential for every Spring Boot developer.

Why Do We Need a Build Tool?

Java applications have many dependencies (libraries). Managing these manually (downloading JARs, adding to classpath) is impossible for real projects. Build tools automate:

  • Downloading dependencies from repositories (Maven Central)
  • Running compilation and tests
  • Packaging applications (JAR or WAR files)
  • Managing transitive dependencies (dependencies of your dependencies)

Maven: The Industry Standard

Maven uses an XML file (pom.xml) to define project configuration. It follows convention over configuration — the source code must be in src/main/java, test code in src/test/java.

Text
<?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>3.1.5</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>17</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Maven Commands (run locally): mvn clean (delete target), mvn compile (compile code), mvn test (run tests), mvn package (create JAR), mvn spring-boot:run (run the app).

Understanding Maven Coordinates

Every dependency is identified by three coordinates:

  • groupId: Organization (com.example, org.springframework)
  • artifactId: Project name (spring-boot-starter-web)
  • version: Specific version (3.1.5)

Maven downloads dependencies from Maven Central Repository.

Gradle: The Modern Alternative

Gradle uses a Groovy or Kotlin DSL (build.gradle) and is more flexible than Maven. It is also generally faster due to incremental builds and build cache.

Text
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.5'
    id 'io.spring.dependency-management' version '1.1.3'
}

group = 'com.example'
version = '1.0.0'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'org.postgresql:postgresql'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}
Gradle Commands (run locally): ./gradlew clean, ./gradlew build, ./gradlew test, ./gradlew bootRun.

Maven vs Gradle: Which to Choose?

Spring Boot supports both. Maven is more widely used in enterprises and has stricter conventions. Gradle is more flexible and often faster for large projects. For learning, Maven is recommended due to simpler XML structure.

Text
Maven Gradle
────────── ──────────
XML DSL   Groovy/Kotlin DSL
Slower    Faster (incremental builds)
Strict    Flexible
Wider adoption  Growing adoption

Creating a Spring Boot Project with Spring Initializr

The easiest way to start is using Spring Initializr.

Bash
# Via web browser: https://start.spring.io
# Select: Maven/Gradle, Java 17, Spring Boot 3.1.x
# Add dependencies: Spring Web, Spring Data JPA, PostgreSQL Driver
# Generate and download the project

# Or via curl (Maven example)
curl https://start.spring.io/starter.zip \
  -d dependencies=web,data-jpa,postgresql \
  -d groupId=com.example \
  -d artifactId=demo \
  -o demo.zip

# Extract and build locally
unzip demo.zip
cd demo
./mvnw spring-boot:run
Local Setup: Run the command above to generate your first Spring Boot project. The Maven wrapper (mvnw) downloads Maven automatically.

Knowledge Check

Ready to test your understanding of 4. Maven & Gradle Build Systems?

Back