Maven 依赖管理

Maven 依赖管理(Dependency Management)完全解析(2025 最新版)

一句话总结
<dependencyManagement> = 统一版本 + 不自动引入<dependencies> = 实际引入 + 可继承两者配合 = 版本冲突 0 容忍!


一、核心概念对比

特性<dependencies><dependencyManagement>
是否自动引入Yes 自动引入No 仅声明版本
子模块继承No 不继承Yes 自动继承版本
作用范围当前模块父 POM → 所有子模块
典型场景普通项目多模块 / 企业级
graph TD
    A[父 POM] --> B[dependencyManagement]
    B --> C[子模块 A]
    B --> D[子模块 B]
    C --> E[dependencies: 无需写 version]
    D --> E

二、<dependencyManagement> 实战(多模块神器)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.enterprise</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <properties>
        <spring-boot.version>3.3.5</spring-boot.version>
        <lombok.version>1.18.34</lombok.version>
        <mysql.version>8.0.33</mysql.version>
    </properties>

    <!-- 统一版本管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot BOM(推荐) -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 自定义版本 -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

子模块 pom.xml

<parent>
    <groupId>com.enterprise</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0</version>
</parent>

<artifactId>service-user</artifactId>

<dependencies>
    <!-- 无需写 version!自动从父继承 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

三、<dependencies> vs <dependencyManagement> 对比示例

配置位置子模块是否引入版本来源
<dependencies>Yes 自动引入父版本
<dependencyManagement>No 需手动声明父版本
<dependencies>Yes本地或父

最佳实践父 POM 用 <dependencyManagement>,子模块用 <dependencies>


四、BOM(Bill of Materials)导入

<dependencyManagement>
    <dependencies>
        <!-- 导入 Spring Boot 所有依赖版本 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.3.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- 导入 Spring Cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

优势零配置,所有 Spring 组件版本自动对齐!


五、依赖范围(Scope)管理

Scope传递性编译测试运行
compileYesYesYesYes
providedYesYesYesNo
runtimeYesNoYesYes
testNoNoYesNo
<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <scope>provided</scope> <!-- 容器提供 -->
</dependency>

六、依赖冲突解决(3 大策略)

策略说明示例
路径最近优先依赖路径最短的版本胜出A→B→C(1.0) 胜过 A→D→E→C(2.0)
声明顺序优先同路径,先声明的胜出先写 1.0 → 后写 2.0 → 选 1.0
手动锁定<dependencyManagement> 强制版本统一用 2.0
# 查看冲突
mvn dependency:tree -Dverbose

七、依赖分析命令

命令作用
mvn dependency:tree依赖树
mvn dependency:analyze未使用 / 重复依赖
mvn dependency:purge-local-repository清理本地缓存
mvn versions:display-dependency-updates检查可升级版本

八、最佳实践清单

实践配置
统一版本<properties> + <dependencyManagement>
父 POM 管理spring-boot-dependencies BOM
禁止 SNAPSHOTmaven-enforcer-plugin
排除冲突<exclusions>
定期检查mvn versions:display-dependency-updates
<!-- 禁止 SNAPSHOT -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.5.0</version>
    <executions>
        <execution>
            <goals><goal>enforce</goal></goals>
            <configuration>
                <rules>
                    <requireReleaseDeps>
                        <message>No Snapshots Allowed!</message>
                    </requireReleaseDeps>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

九、完整示例:企业级 parent/pom.xml

<dependencyManagement>
    <dependencies>
        <!-- Spring Boot BOM -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.3.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- 自定义组件 -->
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>common-utils</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

十、一句话总结

“父 POM 管版本,子模块管引入,BOM 管生态,enforcer 管规范”


恭喜!你已掌握 Maven 依赖管理!


下一步推荐

方向内容
Maven Release 插件自动版本升级
Nexus 版本策略SNAPSHOT vs RELEASE
IDEA 依赖分析Maven Helper 插件

需要 完整企业级 parent POM 模板
回复 Enterprise Parent 立即获取 GitHub 链接!

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注