Apache Maven is a build automation and project management tool. It uses a Project Object Model (POM) to describe a project, manage dependencies, and run standardized build phases.

Maven Coordinates

Every Maven artifact is identified by coordinates:

  <groupId>com.example</groupId>       <!-- Organization -->
<artifactId>my-app</artifactId>       <!-- Project name -->
<version>1.0.0-SNAPSHOT</version>   <!-- Version -->
<packaging>jar</packaging>           <!-- jar, war, pom -->
  

Minimal POM

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

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

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>
  

Standard Directory Layout

  my-app/
├── pom.xml
├── src/
│   ├── main/
│   │   ├── java/          # Source code
│   │   └── resources/     # Config files, properties
│   └── test/
│       ├── java/          # Test code
│       └── resources/     # Test resources
└── target/                # Build output (generated)
  

Lifecycle Phases

Maven defines three built-in lifecycles. The most used is the default lifecycle:

Phase Description
validate Validate project structure
compile Compile source code
test Run unit tests
package Create JAR/WAR
verify Run integration checks
install Install to local repository (~/.m2)
deploy Deploy to remote repository
  mvn compile          # compile only
mvn test             # compile + run tests
mvn package          # compile + test + create JAR
mvn install          # package + install locally
mvn clean package    # clean target/ then package
  

Common Commands

  mvn archetype:generate     # create new project from template
mvn dependency:tree        # show dependency tree
mvn versions:display-dependency-updates  # check for updates
mvn -X package              # debug output
mvn -DskipTests package     # skip tests
mvn -Dmaven.test.skip=true package  # skip test compilation too
  

Parent POM and Inheritance

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

Inheriting from a parent POM provides default plugin versions, dependency management, and properties.

Multi-Module Projects

  <!-- parent pom.xml -->
<packaging>pom</packaging>
<modules>
    <module>core</module>
    <module>web</module>
    <module>api</module>
</modules>
  
  mvn clean install   # builds all modules in dependency order
  

Best Practices

  • Follow the standard directory layout — do not customize without reason
  • Use a parent POM (Spring Boot, company BOM) for consistent versions
  • Keep pom.xml clean — extract versions to <properties>
  • Run mvn dependency:tree regularly to detect conflicts
  • Use -SNAPSHOT for development, release versions for production