POM.xml helps manage your project settings and libraries in one place. Dependencies tell your project which extra tools it needs to work.
POM.xml and dependencies in Spring Boot
Introduction
Syntax
Spring Boot
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myapp</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>group.name</groupId> <artifactId>library-name</artifactId> <version>1.2.3</version> </dependency> </dependencies> </project>
The groupId, artifactId, and version uniquely identify your project.
Each dependency block tells Maven which library to download and use.
Examples
Spring Boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.0</version>
</dependency>Spring Boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>Spring Boot
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>Sample Program
This POM.xml sets up a Spring Boot web app with testing tools. The parent manages versions so you don't have to specify them.
Spring Boot
<?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 http://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.0.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demoapp</artifactId> <version>0.1.0</version> <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> </dependencies> </project>
Important Notes
Always use the Spring Boot starter parent to simplify version management.
Dependencies inside <dependencies> tell Maven what libraries to include.
Scopes like test or runtime control when the dependency is used.
Summary
POM.xml organizes your project and its libraries in one file.
Dependencies list the extra tools your project needs.
Using Spring Boot starter parent helps manage versions easily.
Practice
1. What is the main purpose of the
pom.xml file in a Spring Boot project?easy
Solution
Step 1: Understand the role of
Thepom.xmlpom.xmlfile is used to organize project settings and list needed libraries.Step 2: Identify what
It does not contain Java code, database data, or server settings directly.pom.xmldoes not doFinal Answer:
To manage project configuration and dependencies -> Option BQuick Check:
Project setup = Manage dependencies [OK]
Hint: Think of
pom.xml as your project's shopping list [OK]Common Mistakes:
- Confusing
pom.xmlwith Java source files - Thinking it stores runtime data
- Assuming it configures server directly
2. Which of the following is the correct way to add the Spring Boot starter web dependency in
pom.xml?easy
Solution
Step 1: Check the correct groupId and artifactId for Spring Boot web starter
The official groupId isorg.springframework.bootand artifactId isspring-boot-starter-web.Step 2: Compare options to official naming
Only \n org.springframework.boot\n spring-boot-starter-web\n matches the exact correct groupId and artifactId.Final Answer:
Correct dependency XML withorg.springframework.bootandspring-boot-starter-web-> Option AQuick Check:
Correct groupId/artifactId = \n org.springframework.boot\n spring-boot-starter-web\n [OK]
Hint: Remember exact spelling of groupId and artifactId [OK]
Common Mistakes:
- Swapping groupId and artifactId
- Misspelling 'spring-boot-starter-web'
- Using wrong groupId like 'springframework.boot'
3. Given this
pom.xml snippet, what will happen when you run mvn clean install?
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
medium
Solution
Step 1: Understand Spring Boot starter dependencies
Spring Boot starters manage versions internally, so explicit version is not needed if parent is set.Step 2: Recognize what spring-boot-starter-data-jpa provides
It includes JPA API and an implementation like Hibernate automatically.Final Answer:
The project will include JPA support and related libraries automatically -> Option CQuick Check:
Spring Boot starters handle versions and implementations [OK]
Hint: Spring Boot starters bundle needed libs and versions [OK]
Common Mistakes:
- Thinking version tag is always required
- Assuming only API without implementation
- Believing scope tag is mandatory for dependencies
4. You added this dependency to your
pom.xml but get a build error:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.0.RELEASE</version> </dependency>What is the likely cause?
medium
Solution
Step 1: Understand Spring Boot parent version management
Spring Boot starter parent manages dependency versions automatically.Step 2: Identify problem with manually specifying version
Manually adding version can cause conflicts and build errors.Final Answer:
Version conflicts because Spring Boot parent manages versions -> Option DQuick Check:
Let parent manage versions to avoid conflicts [OK]
Hint: Avoid manual versions if using Spring Boot starter parent [OK]
Common Mistakes:
- Adding version manually when parent manages it
- Thinking scope is required for starters
- Misplacing dependencies outside
dependencies
5. You want to add a custom library that is not in Maven Central to your Spring Boot project. Which is the best way to include it in your
pom.xml?hard
Solution
Step 1: Understand how Maven resolves dependencies
Maven downloads dependencies from repositories defined inpom.xmlor local repo.Step 2: Identify best practice for custom libraries
Uploading to a remote repository and adding its URL inpom.xmlis clean and maintainable.Step 3: Why other options are less ideal
Local install requires manual setup; system scope is discouraged; fake IDs cause errors.Final Answer:
Upload the library to a remote repository and add its repository URL and dependency with version -> Option AQuick Check:
Use remote repo for custom libs [OK]
Hint: Use remote repo URLs for custom dependencies [OK]
Common Mistakes:
- Using system scope which is deprecated
- Skipping repository URL for custom libs
- Not specifying version for custom dependencies
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
