Skip to main content

Maven Fundamentals

Understanding pom.xml

Introduction

In Apache Maven, POM stands for Project Object Model. It’s the heart of any Maven project and is defined in a file called pom.xml located at the project’s root directory.

The pom.xml file contains all the project configuration details, including:

  • GAV coordinates (GroupId, ArtifactId, Version)
  • Dependencies and their versions
  • Build settings
  • Plugins for additional tasks
  • Custom properties for reuse

Understanding the POM is critical because Maven uses it to:

  • Identify your project uniquely
  • Resolve dependencies automatically
  • Define build lifecycle and customization

1. The Structure of POM.xml

A basic Maven pom.xml might look like this:

<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>

    <!-- GAV -->
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>My Maven Application</name>
    <description>Sample Maven project for demonstration</description>
    
</project>

2. GAV – GroupId, ArtifactId, Version

These three elements uniquely identify a Maven project:

Element Description
groupId Unique identifier for your project’s organization or company (e.g., com.example, org.apache.maven)
artifactId The name of the project or module (e.g., my-app, payment-service)
version The version of the artifact being built (e.g., 1.0.0, 1.0.0-SNAPSHOT)

Example:

<groupId>com.javahandbook</groupId>
<artifactId>maven-tutorial</artifactId>
<version>1.0.0</version>

📌 Note: Together, GAV acts like a coordinate system for Maven to fetch or publish artifacts in repositories.

3. Properties in POM.xml

Properties allow you to define reusable values in your POM.
They’re especially useful for version management and build customization.

<properties>
    <java.version>17</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <spring.boot.version>3.1.0</spring.boot.version>
</properties>

You can reference these properties anywhere in your POM using ${property.name}.

4. Dependencies

Dependencies are libraries your project needs to compile, test, or run.
They are declared in the <dependencies> section.

<dependencies>
    <!-- Example: Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
</dependencies>

Dependency Scopes

  • compile (default) – Required for compilation and runtime
  • provided – Required for compilation but provided at runtime (e.g., servlet API in Tomcat)
  • runtime – Needed only at runtime, not for compilation
  • test – Used only for testing (e.g., JUnit)

5. Plugins

Maven uses plugins to extend its capabilities. Plugins define goals that can be executed during the build lifecycle.

Example: Compiler Plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Other popular plugins:

  • maven-surefire-plugin – Runs unit tests
  • maven-jar-plugin – Creates JAR files
  • maven-shade-plugin – Creates fat/uber JARs

6. Build Section

The <build> section in POM defines:

  • Plugins and their configurations
  • The output directory for compiled code
  • Custom build profiles

Example:

<build>
    <finalName>my-app</finalName>
    <plugins>
        <!-- Plugins go here -->
    </plugins>
</build>

Best Practices for POM.xml

✅ Keep dependency versions in <properties> for easy upgrades
✅ Use dependencyManagement in parent POMs for version control in multi-module projects
✅ Remove unused dependencies to speed up builds
✅ Use meaningful GAV coordinates to avoid conflicts

Key Takeaways

  • pom.xml is the blueprint of a Maven project
  • GAV uniquely identifies your project in repositories
  • Properties help with reusability and maintainability
  • Dependencies and plugins define what your project needs to run and how it builds
  • The build section customizes Maven’s lifecycle for your needs