The pom.xml file (Project Object Model) is the single configuration file that tells Apache Maven what your project is, what it depends on and how to build it. Maven reads it on every run to resolve dependencies, run lifecycle phases and configure plugins. Without a pom.xml in the current directory, Maven has nothing to build and stops with an error.
Almost everything you do in a Maven project, from adding a library to publishing a release, ends up as a change to this one file. The examples below target Maven 3.9.x and Java 21.
A POM is an XML document with a root <project> element. Its main parts are:
| Section | Purpose |
|---|---|
Coordinates (groupId, artifactId, version) | Uniquely identify the artifact in any Maven repository |
packaging | Type of artifact to produce: jar (default), war, pom and others |
properties | Reusable values such as the Java release or library versions |
dependencies | Libraries the project needs |
dependencyManagement | Central place to pin versions without adding the dependency itself |
build | Plugins, resources and their configuration |
parent and modules | Inheritance and multi-module structure |
profiles | Environment-specific overrides |
repositories and distributionManagement | Where to download from and publish to |
This POM builds a Java 21 application and runs JUnit 5 tests.
The modelVersion is always 4.0.0 for Maven 3 projects. The coordinates com.example:demo-app:1.0.0-SNAPSHOT identify the artifact. A version ending in -SNAPSHOT marks work in progress that Maven may re-download when it changes. The maven.compiler.release property makes the compiler plugin compile against the Java 21 API, which is safer than the older source and target pair.
Maven combines your POM with the built-in Super POM and any parent POM to produce the effective POM. You can print the result with:
The effective POM includes the default plugin bindings for the chosen packaging type. For jar packaging, the default lifecycle runs these phases in order:
validate: check the project is correct.compile: compile the main sources.test: run unit tests.package: build the JAR.verify: run integration checks.install: copy the artifact to your local repository (~/.m2/repository).deploy: publish it to a remote repository.Running mvn package executes every phase up to and including package. The POM decides which plugin goals run in each phase and with what settings.
Each <dependency> is resolved from Maven Central (or any configured repository) together with its transitive dependencies. The scope controls where the library is on the classpath.
| Scope | Compile | Test | Runtime | Typical use |
|---|---|---|---|---|
compile (default) | yes | yes | yes | Application libraries |
provided | yes | yes | no | Servlet API supplied by the container |
runtime | no | yes | yes | JDBC drivers |
test | no | yes | no | JUnit, Mockito |
In multi-module projects, put versions in dependencyManagement so that every module uses the same ones:
Importing a bill of materials (BOM) like this pins hundreds of compatible versions at once. Modules then declare dependencies without a <version>. Use mvn dependency:tree to see why a transitive library is on the classpath.
Plugins do the real work. Configure them in <build><plugins>:
Always pin plugin versions so builds stay reproducible. Profiles switch parts of the POM on demand, for example with mvn -Pci verify:
A child module declares a <parent> element to inherit properties, dependency management and plugin configuration. A parent project uses <packaging>pom</packaging> and lists its children in <modules>.
No. Maven needs a POM to know the project coordinates and lifecycle. You can point to another file with mvn -f path/to/pom.xml, but one must exist.
The pom.xml describes one project and is committed to version control. settings.xml holds user or machine settings such as mirrors, proxy and credentials, and lives in ~/.m2.
Usually a dependencyManagement section, an imported BOM or a parent POM overrides it. Run mvn help:effective-pom and mvn dependency:tree to see the resolved value.
It plays the same role but uses a Groovy or Kotlin script instead of declarative XML. Maven favours convention and a fixed lifecycle, while Gradle favours programmable builds.
The pom.xml is the declarative description of a Maven project: its coordinates, dependencies, plugins, properties and inheritance. Maven merges it with the Super POM and any parent into the effective POM, then runs lifecycle phases against it. Keep versions in properties or dependencyManagement, pin plugin versions and inspect the result with help:effective-pom and dependency:tree.