Maven POM

什么是 POM?

POM(Project Object Model,项目对象模型)是 Maven 的核心概念。它是一个 XML 文件,包含了项目的基本信息、依赖关系、构建配置等。

核心概念:POM 文件名必须是 pom.xml,位于项目根目录下。

POM 基本结构

<?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 
         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>
    
    <!-- 项目信息 -->
    <name>My Project</name>
    <description>A sample Maven project</description>
    <url>http://www.example.com</url>
    
    <!-- 属性 -->
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <!-- 依赖 -->
    <dependencies>
        <!-- 依赖项 -->
    </dependencies>
    
    <!-- 构建配置 -->
    <build>
        <!-- 构建配置 -->
    </build>
    
</project>

POM 元素详解

1. 项目坐标

项目坐标唯一标识一个项目:

元素 描述 示例
groupId 组织标识符,通常是反向域名 com.example
artifactId 项目标识符 my-project
version 项目版本 1.0.0
packaging 打包类型 jar, war, pom

2. 项目信息

<name>项目名称</name>
<description>项目描述</description>
<url>项目主页</url>
<inceptionYear>2023</inceptionYear>

<licenses>
    <license>
        <name>Apache License, Version 2.0</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    </license>
</licenses>

<developers>
    <developer>
        <id>john</id>
        <name>John Doe</name>
        <email>john@example.com</email>
        <organization>Example Corp</organization>
    </developer>
</developers>

3. 属性配置

使用 properties 元素定义可重用的属性:

<properties>
    <!-- Java 版本 -->
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    
    <!-- 编码 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    
    <!-- 依赖版本 -->
    <spring.version>5.3.21</spring.version>
    <junit.version>5.8.2</junit.version>
</properties>

使用属性:${spring.version}

4. 依赖管理

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

依赖范围(Scope)

范围 描述 编译 测试 运行
compile 默认范围,所有阶段都可用
test 仅测试时可用
provided 编译和测试时可用,运行时由容器提供
runtime 测试和运行时可用
system 类似 provided,但需要显式指定 jar 路径

构建配置

<build>
    <!-- 源码目录 -->
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    
    <!-- 资源目录 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    
    <!-- 插件配置 -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>

父 POM 和继承

继承机制:Maven 支持 POM 继承,子项目可以继承父项目的配置。

父 POM 示例

<!-- parent-pom.xml -->
<project>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>5.8.2</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

子 POM 示例

<!-- child-pom.xml -->
<project>
    <modelVersion>4.0.0</modelVersion>
    
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
    </parent>
    
    <artifactId>child-project</artifactId>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!-- 版本从父 POM 继承 -->
        </dependency>
    </dependencies>
</project>

多模块项目

<!-- 父项目 POM -->
<project>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>multi-module-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    
    <modules>
        <module>module-a</module>
        <module>module-b</module>
        <module>module-c</module>
    </modules>
</project>

POM 最佳实践

常用 POM 模板

<?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 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    
    <name>My Application</name>
    <description>A sample Maven application</description>
    
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.8.2</junit.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M7</version>
            </plugin>
        </plugins>
    </build>
    
</project>