乐闻世界logo
搜索文章和话题

What are the core elements of Maven's POM file? How to configure the POM file?

2月18日 21:28

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:

  1. Project Coordinates:

    • groupId: Project group ID, usually the reverse of the company or organization domain name
    • artifactId: Project artifact ID, usually the project name
    • version: Project version number, following semantic versioning specification
    • packaging: Packaging type, such as jar, war, pom, ear, etc.
  2. Project Information:

    • name: Project display name
    • description: Project description
    • url: Project homepage URL
    • licenses: Project licenses
    • developers: Developer information
    • scm: Source code management information
  3. Dependency Management:

    • dependencies: Project dependency list
    • dependencyManagement: Dependency version management
    • exclusions: Dependency exclusions
  4. Build Configuration:

    • build: Build configuration
    • plugins: Plugin configuration
    • resources: Resource configuration
    • filters: Resource filters
  5. Other Configurations:

    • properties: Property definitions
    • profiles: Configuration files
    • repositories: Repository configuration
    • parent: 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:

  1. Parent POM configuration
  2. Current POM configuration
  3. Activated Profile configuration
  4. Command line parameter configuration

Understanding the structure and configuration of POM files is crucial for effectively using Maven.

标签:Maven