Skip to main content

Command Palette

Search for a command to run...

Beginner's Guide to pom.xml with Real-Life Examples

A practical explanation of Maven's pom.xml — dependencies, scopes, plugins, and how pom.xml organizes builds. Includes Java Spring library system

Updated
4 min read
Beginner's Guide to pom.xml with Real-Life Examples
J

Software Engineer

Introduction to pom.xml:

pom.xml is the main file for a Maven Java project. It defines the project coordinates, such as group, artifact, and version, manages all these aspects, and configures build plugins like compiler, test, runner, and packaging. In this blog, I will explain everything about pom.xml using the example of a Library Management System (Spring + JDBC + PostgreSQL), showing why it makes development easier. If you are a Java developer, you’ll be amazed at how effortlessly it handles dependencies. By using pom.xml, you get automatic dependency resolution, reproducible builds, test runs, and packaging.

Why pom.xml simplifies life:

Maven’s pom.xml isn’t just a config file — it’s your project’s control center. Here’s how it streamlines development:

  • Single centralized dependency management — add one <dependency>, and Maven pulls it and its transitive libraries.

  • Standard build lifecycle (clean, compile, test, package, install, deploy) — no ad-hoc scripts.

  • Plugins (compiler, surefire, spring-boot, shade) let you customize compilation, testing, and packaging.

  • Profiles let you switch environment-specific settings (dev vs prod) without changing code.

Dependencies I used in my Spring project(Library Management System):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.39</version>
</dependency>

org.springframework:spring-context:5.3.39 This is the core Spring container module, responsible for Inversion of Control (IoC), bean lifecycle management, and dependency injection. It provides the ApplicationContext, which acts as a central registry for all configured beans. If you're new to Spring, think of it as a framework that wires together your components (beans) based on configuration — making your application modular, testable, and easy to manage.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.39</version>
</dependency>

org.springframework:spring-jdbc:5.3.39 This module provides JDBC support through helpers like JdbcTemplate and DataSourceUtils, simplifying query execution, result mapping (RowMapper), and exception handling. Without Spring, you'd manually manage connections and write verbose DAO code. With Spring JDBC, much of that boilerplate is handled for you — making data access cleaner and more maintainable.

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>

org.junit.jupiter:junit-jupiter:5.10.0 (scope: test) This is the core API and engine for JUnit 5, used to write and run unit tests in Java. It provides annotations like @Test, lifecycle hooks, and integration with Maven’s test phase — making it easy to structure and automate your test cases.

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.12.0</version>
    <scope>test</scope>
</dependency>

org.mockito:mockito-core:5.12.0 (scope: test) Mockito is a popular Java library for creating mock objects in unit tests. It allows you to simulate dependencies and verify interactions, making it easier to test components in isolation without relying on real implementations.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>test</scope>
</dependency>

javax.servlet:javax.servlet-api:4.0.1 (scope: test) This dependency provides the core Servlet API, including interfaces like HttpServletRequest and HttpServletResponse. It’s essential for unit testing web controllers in Spring MVC applications, allowing you to simulate HTTP requests and responses without deploying to a servlet container.

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.7.3</version>
</dependency>

org.postgresql:postgresql:42.7.3 This is the official JDBC driver for PostgreSQL, enabling Java applications to connect to and interact with PostgreSQL databases. It’s required by DataSource configurations in Spring JDBC or JPA setups to perform database operations.

Dependencies: If you are working with a SpringBoot Project:

Parent Declaration:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.4</version> 
    <relativePath/> 
</parent>

Without the parent, you'd have to manually manage plugin versions, dependency versions, and lifecycle configurations — which can be error-prone and time-consuming. The parent pom.xml acts like a trusted blueprint for building Spring Boot apps efficiently.

Plugins I used in my project(Library Management System):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <source>17</source>
        <target>17</target>
    </configuration>
</plugin>

maven-compiler-plugin This plugin handles Java compilation during the Maven build process. The <source> and <target> tags define the language level — in your case, Java 17. Make sure your system’s JDK matches this version to avoid compatibility issues.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <useModulePath>false</useModulePath>
    </configuration>
</plugin>

maven-surefire-plugin This plugin runs unit tests during the Maven build process. In some cases, adding useModulePath=false helps avoid issues related to JPMS (Java Platform Module System), especially when working with older or non-modular libraries. If your project uses the traditional classpath setup, this configuration may not be necessary.

Conclusion

Maven’s pom.xml. It isn’t just a configuration file — it’s the backbone of your Java project’s build and dependency management. Whether you're working with Spring Boot, JDBC, JUnit, or packaging tools, a well-structured pom.xml simplifies development, testing, and deployment. By understanding each plugin and dependency, you gain full control over your project lifecycle and ensure maintainability across environments.

If you're building a Library Management System or any modular Java application, mastering pom.xml is a must — and once you do, your workflow becomes smoother, cleaner, and far more scalable.

📦 GitHub Repository

You can explore the complete project here: Library Management System