Maven pom.xml for a Java Project
The Maven Project Object Model (pom.xml) is the build configuration file for Java projects, defining dependencies, plugins, and build lifecycle settings. This example shows a Spring Boot application with web and data JPA dependencies, the Maven Compiler plugin for Java 21, and the Spring Boot Maven plugin for creating an executable JAR. The XML formatter validates element nesting and namespace declarations, which Maven is strict about. Always specify dependency versions or use a Bill of Materials (BOM) import to avoid version conflicts.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
</parent>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>FAQ
- What is the difference between Maven and Gradle?
- Maven uses XML configuration and a fixed build lifecycle. Gradle uses Groovy or Kotlin DSL with a flexible task graph. Gradle is generally faster due to incremental builds and build caching; Maven is more predictable for enterprise settings.
- What does a BOM import do in Maven?
- A Bill of Materials import (scope=import, type=pom) pulls in a set of pre-validated dependency versions from a parent project. Spring Boot's starter-parent includes a BOM that pins compatible library versions so you do not specify versions individually.
- How do I add a local JAR as a Maven dependency?
- Use system scope with the systemPath pointing to the JAR file. This is a workaround; the recommended approach is to install the JAR into your local Maven repository with mvn install:install-file.
Related Examples
An XML sitemap tells search engine crawlers which pages to index, how often they...
Cargo.toml for a Rust ProjectCargo.toml is the manifest file for Rust projects, defining the package metadata...
Dockerfile for a Node.js ApplicationA well-structured Node.js Dockerfile uses a multi-stage build to keep the final ...