Skip to content

pom.xml 配置详解

TIP

pom.xml 是 Maven 项目的核心配置文件,描述了项目的坐标、依赖、构建配置等信息。

基本配置

xml
<project>
    <modelVersion>4.0.0</modelVersion>

    <!-- 项目坐标(唯一标识) -->
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>  <!-- jar/war/pom -->

    <name>My App</name>
    <description>示例项目</description>
</project>

依赖管理

xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.7.0</version>
        <!-- scope: compile/test/provided/runtime/system -->
        <scope>compile</scope>
        <!-- 排除传递依赖 -->
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

属性配置

xml
<properties>
    <java.version>11</java.version>
    <spring.version>5.3.20</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

多模块项目

xml
<!-- 父工程 -->
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<modules>
    <module>common</module>
    <module>service</module>
    <module>web</module>
</modules>

<!-- 统一依赖管理 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>