Format Maven POM.xml — Dependencies, Plugins, and Profiles
Format and structure Maven POM.xml files for readability. Learn the standard POM element ordering, dependency management, plugin configuration, and profile organization.
Detailed Explanation
Formatting Maven POM.xml
The Maven POM (Project Object Model) is the fundamental XML configuration file for Maven-based Java projects. A well-formatted POM file is essential for team collaboration and project maintenance.
Standard POM Structure
<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-project</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<java.version>17</java.version>
<spring.version>6.1.0</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Recommended Element Order
The Maven documentation recommends a specific ordering of top-level elements:
modelVersionparentgroupId,artifactId,version,packagingname,description,urlpropertiesdependencyManagementdependenciesbuild(plugins, resources)profiles
Following this convention makes POMs predictable across projects.
Dependency Formatting
Each dependency block should be consistently indented. Group dependencies logically: compile dependencies first, then test dependencies, then provided/runtime:
<dependencies>
<!-- Compile dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Plugin Configuration
Build plugins can carry complex configuration blocks. Proper formatting prevents configuration errors:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
Multi-Module POMs
Parent POMs with <modules> sections benefit greatly from formatting, as they coordinate multiple child projects and can become complex with inherited dependency management and plugin management sections.
Use Case
Maven POM formatting is essential for Java development teams reviewing dependency changes in pull requests, resolving merge conflicts in POM files, auditing dependency versions for security vulnerabilities, and onboarding new developers who need to understand the project's build configuration.
Try It — XML Formatter
Related Topics
XML Indentation Styles — Spaces, Tabs, and Depth Control
Basic Formatting
Format Spring Framework XML Configuration Files
Configuration
Format AndroidManifest.xml — Permissions, Activities, and Intents
Configuration
Format and Pretty-Print XML Online
Basic Formatting
XML Namespace Validation — Prefixes, URIs, and Scope
Validation