Spring Boot services · Chapter 1 of 41
Maven and the pom.xml
By the end of this chapter you can read and write a Spring Boot project's pom.xml: its name, its parent, its starters and a profile. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Spring foundations
- 6 tasks
- Spring Boot
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
Tapri Cloud needs a web server, JSON, validation, a database driver and test tools: dozens of libraries, each with its own version. Meera wants every laptop and the server to build with exactly the same ones, and Ravi wants to know which Jackson goes with which Spring.
Tapri Counter was a few files you ran with java Receipt.java. With libraries, you would copy jars to every machine by hand and guess which versions fit together; a wrong guess fails at runtime.
The lesson
Coordinates
Every project and library has three coordinates: a groupId (who made it, usually a reversed domain), an artifactId (its name) and a version. The JWT library used later in this lab:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.13.0</version>
</dependency>
On one line: io.jsonwebtoken:jjwt-api:0.13.0. A version ending in -SNAPSHOT means "still changing". Your starter pom is still called com.example:demo.
The parent picks the versions
A pom can inherit from another pom: a <parent> element names it by its coordinates, plus an empty <relativePath/> so Maven fetches it from the repository, not the folder above. Spring Boot's parent gives you:
- Dependency management: several hundred libraries (Spring, Jackson, Tomcat, JUnit) with versions tested together.
- Plugin defaults:
spring-boot-maven-plugin, already in your pom, needs no version. - Settings: a
java.versionproperty (17 unless you set it) that tells the compiler which Java release to build for.
The lab's own build inherits from Boot 4.1.1. Some of the versions it chose:
$ ls /opt/check/lib | grep -E '^(spring-boot-starter-webmvc|spring-webmvc|tomcat-embed-core|junit-jupiter-api|mockito-core|jjwt-api)-'
jjwt-api-0.13.0.jar
junit-jupiter-api-6.0.3.jar
mockito-core-5.23.0.jar
spring-boot-starter-webmvc-4.1.1.jar
spring-boot-starter-webmvc-test-4.1.1.jar
spring-webmvc-7.0.9.jar
tomcat-embed-core-11.0.24.jar
Only jjwt-api is not on Boot's list. Press Next to follow two dependencies:
The Check wants the exact version. A parent at 4.1 gets:
[the parent's <version>] expected: "4.1.1" but was: "4.1"
A tag left open fails every task with the parser's message:
SAXParseException: The element type "parent" must be terminated by the matching end-tag "</parent>".
Starters and scopes
A starter is a dependency that brings a whole feature. Its jar is almost empty (spring-boot-starter-webmvc-4.1.1.jar is 4749 bytes): it is a list of other libraries, here Spring MVC, Jackson and Tomcat. Under a Boot parent you name a starter without a version:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
The interview point: the parent's dependency management picks the version, so upgrading Spring Boot is one number and every starter moves with it. A hard-coded version stays behind. The Check catches one:
[its <version> (leave it out: the parent manages it)] expected: null but was: "4.1.1"
A scope says where a dependency is needed. The default, compile, means everywhere; runtime means only the running app, as for a database driver:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
test means only for the tests, never in the jar you ship.
Maven profiles
Maven builds in phases (compile, test, package, install and a few more), and asking for one runs every phase before it: mvn package runs the tests first.
A profile is a block of extra settings in a <profiles> element at the end of the project, switched on with -P and its id. This one skips the tests for mvn package -Pfast:
<profiles>
<profile>
<id>fast</id>
<properties>
<skipTests>true</skipTests>
</properties>
</profile>
</profiles>
The Spring Boot plugin reads a property too, spring-boot.run.profiles: the Spring profiles to switch on when Maven starts the app (chapter 5).
In this lab the libraries are already downloaded and run-app compiles each chapter, so you will not run Maven here.
Try it
See the pom and the lab's starters:
cd chapters/01-maven-and-the-pom
cat pom.xml
ls /opt/check/lib | grep spring-boot-starter
Your turn
Your six tasks are at the top; the comments in pom.xml say each task again where its change goes. Traps: the parent is exactly 4.1.1, the starters have no version, and the profile's property goes inside the profile. Kabir asks task 4 in interviews: who picks the starter's version?
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
pom.xml: give the project Tapri Cloud's coordinates
Change the project's own coordinates from com.example:demo to com.tapri:tapri-cloud, keeping the version 0.0.1-SNAPSHOT.
These are the groupId, artifactId and version directly inside project, not the ones inside parent or a dependency.
- Task 2
pom.xml: inherit from spring-boot-starter-parent 4.1.1
Give the project a parent element: groupId org.springframework.boot, artifactId spring-boot-starter-parent, version exactly 4.1.1 (not 4.1), with an empty relativePath so Maven looks the parent up in the repository.
It goes directly inside project, before the project's own coordinates.
- Task 3
pom.xml: build for Java 25 with the java.version property
Set the java.version property, inside the project's properties element, to 25. The starter has it at 17, the parent's default.
- Task 4 · interview question
Interview: add the web starter without a version, and say who picks it
Add a dependency on org.springframework.boot:spring-boot-starter-webmvc inside the dependencies element. Give it no version element (the parent's dependency management picks it) and no scope other than the default, compile.
The test also checks that the project has a parent, so do task 2 first. Be ready to say who picks the starter's version, and what happens when you upgrade Spring Boot.
- Task 5
pom.xml: add the test starter in test scope
Add a dependency on org.springframework.boot:spring-boot-starter-webmvc-test with its scope set to test and no version element.
It brings JUnit, AssertJ, Mockito and MockMvc, which only the tests need, so it must not end up in the jar you ship.
- Task 6
pom.xml: add a prod profile for the Spring prod profile
Add a profiles element at the end of project with one profile whose id is prod. That profile's own properties element sets spring-boot.run.profiles to prod, so that running the app through Maven with the prod profile starts Spring with the prod profile.
The property goes inside the profile, not in the project's main properties element.
What you will learn
- Coordinates: three names for exactly one project or library.
- The parent: spring-boot-starter-parent, which picks tested versions for you.
- Starters and scopes: one dependency per feature, and where it is needed.
- Maven profiles: extra settings switched on by name.
More in Spring foundations
Build "Maven and the pom.xml" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.