Java NotesRohit’s interview study guide
Chapter 11

Maven, Gradle and Project Structure

How a Java project is built: Maven's pom.xml compared with package.json, dependencies and their scopes, the build lifecycle, Gradle, the Spring Boot starter project every later chapter uses, and what's inside the JAR you deploy. Every example was run with Java 25, Maven 3.10.1 and Gradle 9.9.2 through their wrappers.

Try it yourself. You don't install Maven or Gradle: each project carries a wrapper script (./mvnw, ./gradlew) that downloads the right version the first time. The Spring examples start from the starter project in The Spring Boot starter project: make it once, then copy its folder for each example (cp -R shop my-example) and save the example's files over it. Maven prints a lot; the Terminal boxes show Maven's summary only (the dependency tree, the test results, or the errors), and your terminal will show more lines around it.

7 topics
Parts marked Advanced are extra depth. Skip them on a first read or a quick revision.

#Maven compared with npm

Added

In Node.js, package.json lists your dependencies and scripts, npm install downloads packages from the npm registry into node_modules, and npm test runs a script. Maven does the same jobs for Java with different names, and one big difference in philosophy:

npmMavenNote
package.jsonpom.xmlthe project file ("Project Object Model"), in XML
"name", "version"groupId, artifactId, versionthe coordinates: com.example:shop:0.0.1-SNAPSHOT
npm registryMaven Centralwhere libraries are published
node_modules/ per project~/.m2/repository/ sharedeach JAR is downloaded once per machine and shared by every project
"scripts" you writethe lifecycle (compile, test, package)the steps are standard; plugins do the work
npx, Corepack./mvnw (Maven Wrapper)pins the Maven version per project
package-lock.jsonnone by defaultversions in a pom are exact; Spring Boot's parent picks matching versions for you

The philosophy difference: npm runs scripts you write; Maven runs a fixed build lifecycle that every project shares. You don't write "how to compile" or "how to test" in a pom. You say what the project is (coordinates, dependencies, Java version) and Maven already knows the steps: compile src/main/java, compile and run the tests in src/test/java, package a JAR. That's convention over configuration: every Maven project in the world has the same folder layout and the same commands, so you can open any of them and know where things are.

Here's a complete Maven project with no framework: one class, one test, and one dependency (JUnit, for tests only).

pom.xml
<?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>

  <groupId>com.example</groupId>
  <artifactId>pricing</artifactId>
  <version>1.0.0</version>

  <properties>
    <maven.compiler.release>25</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>6.1.3</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
src/main/java/com/example/pricing/Discount.java
package com.example.pricing;

public class Discount {
    public static long apply(long cents, int percent) {
        if (percent < 0 || percent > 100) {
            throw new IllegalArgumentException("percent must be 0-100, was " + percent);
        }
        return cents * (100 - percent) / 100;
    }
}
src/test/java/com/example/pricing/DiscountTest.java
package com.example.pricing;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DiscountTest {
    @Test
    void takesPercentOff() {
        assertEquals(7500, Discount.apply(10000, 25));
    }

    @Test
    void rejectsPercentOver100() {
        assertThrows(IllegalArgumentException.class, () -> Discount.apply(10000, 120));
    }
}
Terminal
$ ./mvnw test
[INFO] Results:
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
What’s happening
  1. The pom's coordinates name the project com.example:pricing:1.0.0. If you published it, another project would depend on it with exactly those three values.
  2. maven.compiler.release is 25, the same as javac --release 25 (chapter 01). The encoding line stops a platform-dependent default from creeping in.
  3. The one dependency is JUnit with <scope>test</scope>: it's on the classpath when tests compile and run, and left out of the main code and the final JAR. npm's equivalent is devDependencies.
  4. Nothing in the pom says where the code is or how to test it. Maven looks in src/main/java and src/test/java by convention, and its default test plugin (Surefire) finds DiscountTest because its name ends in Test.
  5. ./mvnw test runs every lifecycle phase up to test: it downloads JUnit into ~/.m2 the first time, compiles both folders, then runs the tests. The summary says Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 and BUILD SUCCESS.
AdvancedWhy the pom's versions are exact

npm's "^1.2.0" means "1.2.0 or any newer 1.x" and a lock file freezes what you actually got. Maven dependencies are normally written as one exact version, so the pom itself is the lock. The price is that upgrading is manual, which is why tools like Dependabot or Renovate (or ./mvnw versions:display-dependency-updates) matter in Java teams. And when two libraries want different versions of the same third library, Maven doesn't install both as npm would: only one version of a class can be on the classpath, and Maven picks the one nearest to your project in the dependency tree. The next topic shows that tree.

#Dependencies, scopes and the dependency tree

Added

Most of what's on your classpath you never asked for. You add spring-boot-starter-webmvc; it brings Spring MVC, which brings Spring Core, plus Tomcat, plus Jackson for JSON. These are transitive dependencies, exactly like the nested packages under node_modules. The difference is that Java gets one flat classpath, so when two paths bring different versions of the same library, Maven has to choose one.

Each dependency also has a scope that says where it's needed:

ScopeCompile main codeRun testsIn the packaged appTypical example
compile (default)yesyesyesSpring, Jackson
runtimenoyesyesa JDBC driver like H2 or PostgreSQL
testnoyesnoJUnit, Mockito, AssertJ
providedyesyesnoan API the server supplies (servlet API on an external Tomcat)

The tool that answers "where does this library come from?" is dependency:tree, Maven's npm ls. On a Spring Boot project the full tree is long, so filter it with -Dincludes:

src/main/java/com/example/shop/ShopApplication.java
package com.example.shop;

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

@SpringBootApplication
public class ShopApplication {

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

}
Terminal
$ ./mvnw dependency:tree -Dincludes=tools.jackson.core
[INFO] com.example:shop:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-webmvc:jar:4.2.0:compile
[INFO]    \- org.springframework.boot:spring-boot-starter-jackson:jar:4.2.0:compile
[INFO]       \- org.springframework.boot:spring-boot-jackson:jar:4.2.0:compile
[INFO]          \- tools.jackson.core:jackson-databind:jar:3.1.8:compile
[INFO]             \- tools.jackson.core:jackson-core:jar:3.1.8:compile
[INFO] BUILD SUCCESS
$ ./mvnw dependency:tree -Dincludes=com.h2database,org.junit.jupiter:junit-jupiter-api
[INFO] com.example:shop:jar:0.0.1-SNAPSHOT
[INFO] +- com.h2database:h2:jar:2.5.252:runtime
[INFO] \- org.springframework.boot:spring-boot-starter-actuator-test:jar:4.2.0:test
[INFO]    \- org.springframework.boot:spring-boot-starter-test:jar:4.2.0:test
[INFO]       \- org.junit.jupiter:junit-jupiter:jar:6.1.3:test
[INFO]          \- org.junit.jupiter:junit-jupiter-api:jar:6.1.3:test
[INFO] BUILD SUCCESS
What’s happening
  1. This is the starter project unchanged (its main class is shown so the example is complete). Its pom asks for spring-boot-starter-webmvc, never for Jackson.
  2. The first tree shows the path to Jackson: starter-webmvc → starter-jackson → spring-boot-jackson → jackson-databind 3.1.8 → jackson-core. Jackson 3 lives in the tools.jackson group; Jackson 2 was com.fasterxml.jackson, which you'll still see in Boot 3 projects.
  3. Every line ends with the scope Maven resolved. jackson-databind is compile because the starter that brought it is.
  4. The second tree shows two other scopes: the H2 database driver is runtime (the code talks to JDBC interfaces and only needs the driver when it runs), and JUnit's API arrives under test through spring-boot-starter-actuator-test → spring-boot-starter-test. JUnit is at 6.1.3, the version Boot 4.2 manages.
  5. None of these lines needed you to write a version: the Spring Boot parent pom chose all of them. That's the next point.
AdvancedThe parent pom and BOMs: versions that fit together

The starter's pom begins with a <parent>: spring-boot-starter-parent version 4.2.0. A parent pom is inherited configuration, and this one includes a BOM (bill of materials): a list of several hundred libraries with versions that the Spring team has tested together. That's why the starter's dependencies have no <version> tags. Upgrading Spring Boot means changing one number, and Jackson, Hibernate, Tomcat, JUnit and the rest move with it.

If a project can't use the parent (it has its own corporate parent), it imports the same BOM in <dependencyManagement> with <scope>import</scope> instead. Either way, the rule is: let the BOM pick versions, and only override one (with a property such as <jackson-bom.version>) when you have a specific reason, like an urgent security fix.

#The Maven lifecycle and plugins

Added

Maven's build is a fixed sequence of phases. Asking for one phase runs every phase before it:

validate → compile → test → package → verify → install → deploy

So ./mvnw package compiles, runs the tests, and only then builds the JAR; ./mvnw install also copies the JAR into your local ~/.m2 so other projects on your machine can use it; deploy uploads it to a shared repository. clean is a separate lifecycle that deletes target/, which is why you often see ./mvnw clean package.

Phases don't do anything by themselves. Plugins are bound to them: the compiler plugin to compile, Surefire to test, the JAR plugin to package, and in a Spring Boot project the Spring Boot plugin repackages the JAR into an executable one. The mental model: phases are the npm script names that every project agrees on, and plugins are the commands behind them.

The ordering is the useful part: a failing test stops the build before package, so a broken build never produces an artifact.

src/main/java/com/example/shop/PriceCalculator.java
package com.example.shop;

public class PriceCalculator {
    public long totalWithTax(long cents) {
        return cents + cents * 13 / 100;
    }
}
src/test/java/com/example/shop/PriceCalculatorTest.java
package com.example.shop;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class PriceCalculatorTest {
    @Test
    void addsThirteenPercentTax() {
        assertThat(new PriceCalculator().totalWithTax(10000)).isEqualTo(11300);
    }

    @Test
    void roundsHalfCentsUp() {
        assertThat(new PriceCalculator().totalWithTax(50)).isEqualTo(57);
    }
}
Terminal
$ ./mvnw package
[INFO] Results:
[ERROR] Failures: 
[ERROR]   PriceCalculatorTest.roundsHalfCentsUp:15 
expected: 57L
 but was: 56L
[ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 0
[ERROR] BUILD FAILURE
$ ls target/*.jar
ls: target/*.jar: No such file or directory
What’s happening
  1. totalWithTax adds 13% with integer arithmetic. For 10000 cents that's 10000 + 1300 = 11300, which the first test expects.
  2. For 50 cents, 13% is 6.5 cents. Integer division drops the half: 50 * 13 / 100 = 650 / 100 = 6, so the method returns 56. The second test expects 57 (rounding up), so it fails.
  3. ./mvnw package runs compile, then test. The summary names the failing test and line: PriceCalculatorTest.roundsHalfCentsUp:15 with expected: 57L but was: 56L, then Tests run: 3, Failures: 1 (the third is the starter's own ShopApplicationTests) and BUILD FAILURE.
  4. Because test failed, package never ran: ls target/*.jar finds no JAR. Fixing the code (or the test, if rounding down is the real rule) is the only way to get an artifact.
  5. -DskipTests skips running the tests (./mvnw package -DskipTests). It's handy locally when you only need the JAR, and a bad habit in CI.
AdvancedTip: run a single test

#Gradle in five minutes

Added

Gradle is the other build tool you'll meet; Android uses it, many newer backend teams prefer it, and start.spring.io now defaults to it. It uses the same Maven Central repository and the same coordinates, so libraries are identical. The differences are in the build file and the model:

MavenGradle
Build filepom.xml (XML, declarative)build.gradle.kts (Kotlin code) or build.gradle (Groovy)
Modelfixed phases + pluginsa graph of tasks (compileJava, test, bootJar)
Speedrebuilds what the phase saysskips tasks whose inputs didn't change, plus a build cache and a background daemon
Scopescompile, runtime, testimplementation, runtimeOnly, testImplementation, …
Wrapper./mvnw./gradlew

Here's the same Spring Boot starter as a Gradle project, exactly as start.spring.io generates it with the Kotlin build script:

build.gradle.kts
plugins {
	java
	id("org.springframework.boot") version "4.2.0"
	id("io.spring.dependency-management") version "1.1.7"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(25)
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-h2console")
	implementation("org.springframework.boot:spring-boot-starter-actuator")
	implementation("org.springframework.boot:spring-boot-starter-data-jpa")
	implementation("org.springframework.boot:spring-boot-starter-validation")
	implementation("org.springframework.boot:spring-boot-starter-webmvc")
	runtimeOnly("com.h2database:h2")
	testImplementation("org.springframework.boot:spring-boot-starter-actuator-test")
	testImplementation("org.springframework.boot:spring-boot-starter-data-jpa-test")
	testImplementation("org.springframework.boot:spring-boot-starter-validation-test")
	testImplementation("org.springframework.boot:spring-boot-starter-webmvc-test")
	testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.withType<Test> {
	useJUnitPlatform()
}
Terminal
$ ./gradlew test
> Task :compileJava
> Task :processResources
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
BUILD SUCCESSFUL in 21s
4 actionable tasks: 4 executed
$ ./gradlew test
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test UP-TO-DATE
BUILD SUCCESSFUL in 2s
4 actionable tasks: 4 up-to-date
What’s happening
  1. plugins applies the Java plugin and Spring Boot's; io.spring.dependency-management plays the role of Maven's parent BOM, which is why the dependencies have no versions.
  2. The toolchain asks for Java 25: Gradle finds a matching JDK (here the one in JAVA_HOME) and compiles with it, whatever JDK started Gradle.
  3. implementation is Maven's compile scope, runtimeOnly is runtime, and testImplementation is test. The list is the same as the Maven starter's.
  4. The first ./gradlew test runs the task graph in order: compileJava, processResources, classes, compileTestJava, testClasses, test. BUILD SUCCESSFUL, 4 tasks executed (the others had nothing to do or are just groupings).
  5. The second run changes nothing, so every task is UP-TO-DATE and nothing is recompiled or retested: "4 actionable tasks: 4 up-to-date". That incremental behaviour is Gradle's main selling point on big projects. Maven would compile and run the tests again.
AdvancedWhich should you use?

Which should you use? Whatever the team uses; the concepts transfer. For your own projects, Maven is simpler to read and has fewer surprises; Gradle is faster on large multi-module builds and more flexible, at the cost of build scripts that are real code. Interviewers mostly want to know you understand dependencies, scopes and the lifecycle, not one tool's syntax.

#The Spring Boot starter project

Added

Every Spring Boot project starts the same way: from Spring Initializr, the generator at start.spring.io. You pick the build tool, Java version, Spring Boot version and the starters you want, and download a ready-to-run project. IntelliJ and VS Code use the same service behind their "New Spring project" wizards.

The starter used on this site was generated with Maven, Java 25, Spring Boot 4.2.0 and five starters (the command is below). What you get, file by file:

FileWhat it is
pom.xmlthe Spring Boot parent, the five starters this site picked (web, validation, data-jpa, h2, actuator), and a -test starter for each (Boot 4 has one per technology)
mvnw, mvnw.cmd, .mvn/wrapper/the Maven Wrapper (it downloads Maven 3.10.1 on first use)
src/main/java/com/example/shop/ShopApplication.javathe main class: the program's entry point
src/main/resources/application.propertiesconfiguration; it holds one line, spring.application.name=shop
src/test/java/com/example/shop/ShopApplicationTests.javaone test that checks the application starts
src/main/java/com/example/shop/ShopApplication.java
package com.example.shop;

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

@SpringBootApplication
public class ShopApplication {

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

}
Terminal
$ cat src/test/java/com/example/shop/ShopApplicationTests.java
package com.example.shop;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ShopApplicationTests {

	@Test
	void contextLoads() {
	}

}
$ ./mvnw test
[INFO] Results:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
$ curl -s localhost:8080/actuator/health
{"groups":["liveness","readiness"],"status":"UP"}
What’s happening
  1. ShopApplication is an ordinary class with the classic public static void main from chapter 01. SpringApplication.run starts Spring, which scans this package for your classes, configures everything the starters need, and starts an embedded Tomcat web server on port 8080. Chapter 12 opens this up.
  2. @SpringBootApplication marks the class as the configuration starting point; the package it's in (com.example.shop) is where Spring looks for your code, so put every class in this package or below it.
  3. The generated test is a single empty method annotated @SpringBootTest: it passes if the whole application context starts. ./mvnw test runs it: Tests run: 1, Failures: 0.
  4. With the app running (./mvnw spring-boot:run in another terminal; stop it with Ctrl+C), Actuator already answers /actuator/health with {"groups":["liveness","readiness"],"status":"UP"}. The two groups are Kubernetes-style probes, on by default since Boot 4 (chapter 16).
  5. That's a working web service from a generated folder and zero lines of your own code. Every Spring example on this site adds files to a fresh copy of exactly this project.
AdvancedMaking it from the terminal

The same service works from the terminal. This is the exact command that made the starter project used by every Spring example on this site (run on 2026-11-26; it isn't re-run by the checker, because start.spring.io moves on to new versions):

Shell
curl -s -o shop.zip "https://start.spring.io/starter.zip?type=maven-project&language=java&bootVersion=4.2.0&javaVersion=25&groupId=com.example&artifactId=shop&name=shop&packageName=com.example.shop&dependencies=web,validation,data-jpa,h2,actuator"
unzip shop.zip -d shop
cd shop

dependencies=web,validation,data-jpa,h2,actuator picks five starters: Spring MVC for REST APIs (web, which Boot 4 calls webmvc), Bean Validation, Spring Data JPA, the H2 in-memory database, and Actuator for health checks. For a Gradle project use type=gradle-project-kotlin.

#JAR files and the executable Spring Boot JAR

Added

A Node.js service is deployed as source plus node_modules. A Java service is deployed as one file: a JAR. Chapter 01 built a plain JAR with only your classes in it; running that needs every library on the classpath too. Spring Boot's Maven and Gradle plugins build an executable JAR (also called a fat JAR or über-JAR) instead: your classes, every dependency JAR nested inside, and a small launcher that knows how to load classes from nested JARs. java -jar app.jar is the whole deployment.

src/main/java/com/example/shop/ShopApplication.java
package com.example.shop;

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

@SpringBootApplication
public class ShopApplication {

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

}
Terminal
$ ./mvnw -q -DskipTests package
$ ls target/*.jar*
target/shop-0.0.1-SNAPSHOT.jar
target/shop-0.0.1-SNAPSHOT.jar.original
$ unzip -p target/shop-0.0.1-SNAPSHOT.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.5.1
Java-Version: 25
Build-Jdk-Spec: 25
Implementation-Title: shop
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.springframework.boot.loader.launch.JarLauncher
Start-Class: com.example.shop.ShopApplication
Spring-Boot-Version: 4.2.0
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
$ jar tf target/shop-0.0.1-SNAPSHOT.jar | grep -c '^BOOT-INF/lib/.'
79
$ jar tf target/shop-0.0.1-SNAPSHOT.jar | grep -v '^org/' | grep -v '^BOOT-INF/lib/.'
META-INF/
META-INF/MANIFEST.MF
META-INF/services/
META-INF/services/java.nio.file.spi.FileSystemProvider
BOOT-INF/
BOOT-INF/classes/
BOOT-INF/classes/com/
BOOT-INF/classes/com/example/
BOOT-INF/classes/com/example/shop/
META-INF/maven/
META-INF/maven/com.example/
META-INF/maven/com.example/shop/
BOOT-INF/classes/application.properties
BOOT-INF/classes/com/example/shop/ShopApplication.class
META-INF/maven/com.example/shop/pom.xml
META-INF/maven/com.example/shop/pom.properties
BOOT-INF/lib/
BOOT-INF/classpath.idx
BOOT-INF/layers.idx
$ java -Djarmode=tools -jar target/shop-0.0.1-SNAPSHOT.jar list-layers
dependencies
spring-boot-loader
snapshot-dependencies
application
What’s happening
  1. -q keeps Maven quiet, so package prints nothing when it works. target/ then holds two files: shop-0.0.1-SNAPSHOT.jar.original is the plain JAR the JAR plugin made (your classes only), and shop-0.0.1-SNAPSHOT.jar is what the Spring Boot plugin repackaged from it.
  2. The manifest explains how java -jar works: Main-Class is Spring Boot's JarLauncher, not your class. The launcher sets up a class loader for the nested JARs, then calls Start-Class, which is your com.example.shop.ShopApplication.
  3. BOOT-INF/lib/ holds 79 dependency JARs: Spring, Tomcat, Hibernate, Jackson, H2 and the rest. (Test-scoped libraries aren't there.)
  4. Without the launcher's own classes (org/…) and the library JARs, what's left is the layout: your compiled classes and application.properties under BOOT-INF/classes/, plus two index files. classpath.idx fixes the order of the nested JARs on the classpath.
  5. layers.idx groups the contents into layers for container images: dependencies, spring-boot-loader, snapshot-dependencies and application, from least to most often changed. A Docker image built in that order (chapter 16) only rebuilds the small application layer when you change your code, not 79 JARs.

#Your turn: builds

Added
Your turn: build for Java 21

The production servers move to Java 21 before your team can use Java 25. Change the starter so its classes run on Java 21, and prove it from the compiled class file.

Show answer

Change the java.version property in the starter's pom.xml from 25 to 21 (shown here as the whole pom, unchanged otherwise):

pom.xml
<?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>4.2.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>shop</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>shop</name>
	<properties>
		<java.version>21</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-h2console</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webmvc-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>
Terminal
$ ./mvnw -q -DskipTests package
$ javap -v target/classes/com/example/shop/ShopApplication.class | grep major
  major version: 65
What’s happening
  1. In a Spring Boot project, java.version is the one property to change: the parent pom passes it to the compiler as --release.
  2. The build still runs on JDK 25, but --release 21 produces Java 21 class files and rejects any API newer than Java 21.
  3. javap shows major version: 65, which is Java 21 (21 + 44), so a Java 21 server can load it. Boot 4.2 itself supports Java 17 and later, so this is allowed.
Your turn: who brought this library?

Your security scanner flags tomcat-embed-core. You never added Tomcat to the starter's pom. Find out which of your dependencies brought it in, and at which version.

Show answer
src/main/java/com/example/shop/ShopApplication.java
package com.example.shop;

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

@SpringBootApplication
public class ShopApplication {

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

}
Terminal
$ ./mvnw dependency:tree -Dincludes=org.apache.tomcat.embed:tomcat-embed-core
[INFO] com.example:shop:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-webmvc:jar:4.2.0:compile
[INFO]    \- org.springframework.boot:spring-boot-starter-tomcat:jar:4.2.0:compile
[INFO]       \- org.springframework.boot:spring-boot-starter-tomcat-runtime:jar:4.2.0:compile
[INFO]          \- org.apache.tomcat.embed:tomcat-embed-core:jar:11.0.27:compile
[INFO] BUILD SUCCESS
What’s happening
  1. -Dincludes=groupId:artifactId prints only the paths that lead to that library.
  2. The tree shows spring-boot-starter-webmvc → spring-boot-starter-tomcat → … → tomcat-embed-core 11.0.27: Spring MVC needs a web server, and Tomcat is the default embedded one.
  3. To upgrade it for a security fix you'd override the BOM's tomcat.version property, or better, move to the Spring Boot patch release that already has the fixed Tomcat.
Your turn: a test for the tax rounding

In the lifecycle example, the business decides that tax rounds to the nearest cent, half up. Fix totalWithTax so both tests pass and ./mvnw package builds the JAR.

Show answer
src/main/java/com/example/shop/PriceCalculator.java
package com.example.shop;

public class PriceCalculator {
    public long totalWithTax(long cents) {
        return cents + Math.round(cents * 13 / 100.0);
    }
}
src/test/java/com/example/shop/PriceCalculatorTest.java
package com.example.shop;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class PriceCalculatorTest {
    @Test
    void addsThirteenPercentTax() {
        assertThat(new PriceCalculator().totalWithTax(10000)).isEqualTo(11300);
    }

    @Test
    void roundsHalfCentsUp() {
        assertThat(new PriceCalculator().totalWithTax(50)).isEqualTo(57);
    }
}
Terminal
$ ./mvnw package
[INFO] Results:
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
$ ls target/*.jar
target/shop-0.0.1-SNAPSHOT.jar
What’s happening
  1. Dividing by 100.0 instead of 100 makes the division a double: 650 / 100.0 = 6.5, and Math.round(6.5) rounds half up to 7. So 50 cents becomes 57.
  2. For 10000 cents, 130000 / 100.0 = 1300.0, rounded 1300: still 11300.
  3. All three tests pass, so the lifecycle continues past test to package, and ls finds the JAR. (For real money you'd use BigDecimal with an explicit RoundingMode; chapter 02 shows why.)

Written fresh, no doc. Examples were run with Eclipse Temurin 25.0.5.1 and Spring Boot 4.2.0 (Maven 3.10.1); facts come from openjdk.org, docs.oracle.com and spring.io, read 2026-11-26.

RohitDownloads · All chapters

Sync progress across devices

Type the same private phrase on your Mac and your phone, and your Learned ticks follow you between them — on every study site.

The phrase never leaves this device: only a fingerprint of it is sent, and the server stores a fingerprint of that. Anyone who knows the phrase could see or change your ticks, so pick something you don’t use elsewhere.

Sync is on in this browser.

Sync ID

This ID must be the same on every device. If another device shows a different one, its phrase is different (capital letters count): tap “Turn off here” on it and type the phrase again exactly.