On this page
  1. Apache Maven
  2. Overview
  3. Standard Directory Layout
  4. Minimal pom.xml (Java 21 + JUnit 5)
  5. Default Build Lifecycle Phases
  6. Dependency Scopes

Apache Maven

Prev: Jackson | Up: third_party/java | Next: Gradle
Rel: OpenJDK Distributions & Toolchain, Gradle, JUnit 5
Ref: Apache Maven Official Documentation | Maven Central
Tags: #public #prog #java #third-party #build-tool #tier/core


Overview

Apache Maven is a declarative, convention-over-configuration build and dependency management tool driven by a single XML Project Object Model (pom.xml). Every artifact in the Java ecosystem is identified by GAV coordinates: groupId:artifactId:version.

Standard Directory Layout

my-app/
├── pom.xml
├── mvnw / mvnw.cmd             # Maven Wrapper (pins exact Maven version in git)
└── src/
    ├── main/
    │   ├── java/               # Production source code
    │   └── resources/          # Config files, templates (copied to classpath)
    └── test/
        ├── java/               # Unit & integration tests
        └── resources/          # Test fixtures

Minimal pom.xml (Java 21 + JUnit 5)

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>demo-app</artifactId>
  <version>1.0.0-SNAPSHOT</version>

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

  <dependencyManagement>
    <dependencies>
      <!-- BOM (Bill of Materials) aligns transitive versions cleanly -->
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.11.3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Default Build Lifecycle Phases

Running a phase executes all earlier phases in order:

validate → compile → test → package → verify → install → deploy

Command Action
./mvnw test Compiles main & test sources and runs unit tests (Surefire)
./mvnw verify Runs unit tests, packages JAR, and runs integration tests (Failsafe)
./mvnw clean package Deletes target/ and builds a fresh .jar
./mvnw dependency:tree Inspects transitive dependencies and version conflicts

Dependency Scopes

Linked from

Related notes