Maven's POM (Project Object Model) file is the core configuration file of a Maven project, using XML format to describe various information about the project. The POM file is located in the project root directory with the file name pom.xml.
Basic Structure of POM File:
xml<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> <!-- Project Basic Information --> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <!-- Project Description --> <name>My Project</name> <description>Project description</description> <url>https://example.com</url> <!-- Property Configuration --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring.version>5.3.20</spring.version> </properties> <!-- Dependency Management --> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <!-- Dependency Version Management --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- Build Configuration --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> </plugin> </plugins> </build> </project>
Core Elements of POM File:
-
Project Coordinates:
groupId: Project group ID, usually the reverse of the company or organization domain nameartifactId: Project artifact ID, usually the project nameversion: Project version number, following semantic versioning specificationpackaging: Packaging type, such as jar, war, pom, ear, etc.
-
Project Information:
name: Project display namedescription: Project descriptionurl: Project homepage URLlicenses: Project licensesdevelopers: Developer informationscm: Source code management information
-
Dependency Management:
dependencies: Project dependency listdependencyManagement: Dependency version managementexclusions: Dependency exclusions
-
Build Configuration:
build: Build configurationplugins: Plugin configurationresources: Resource configurationfilters: Resource filters
-
Other Configurations:
properties: Property definitionsprofiles: Configuration filesrepositories: Repository configurationparent: Parent POM reference
POM Inheritance:
Child projects can inherit parent POM configurations through the <parent> element:
xml<parent> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> <relativePath>../pom.xml</relativePath> </parent>
Best Practices:
- Use properties to uniformly manage version numbers
- Use dependencyManagement in parent POM to manage dependency versions
- Keep POM files concise, avoid over-configuration
- Use comments to explain complex configurations
- Follow Maven's project structure conventions
- Regularly update dependency versions to maintain project security
POM File Inheritance Order:
- Parent POM configuration
- Current POM configuration
- Activated Profile configuration
- Command line parameter configuration
Understanding the structure and configuration of POM files is crucial for effectively using Maven.