Maven中的<dependencyManagement>作用

文章讲述了Maven中<dependencyManagement>标签的作用,包括版本、范围和排除管理,使得子模块能自动继承父模块的依赖设置,简化项目结构并降低冲突风险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

父 pom.xml

<properties>
	<spring.boot.version>2.2.13.RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
    <dependencies>
        <!-- 只需声明 groupId 和 artifactId,版本号会自动继承 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-legacy</artifactId>
            <version>${spring.boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

子pom.xml

<dependencyManagement>
    <dependencies>
        <!-- 子模块只需声明 groupId 和 artifactId,版本号和排除项会自动继承 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-legacy</artifactId>
        </dependency>
        <!-- 其他依赖... -->
    </dependencies>
</dependencyManagement>

在 Maven 中,<dependencyManagement> 元素位于项目的 pom.xml文件中,它扮演了一个集中管理依赖版本的角色。具体作用包括:

  1. 版本统一管理:<dependencyManagement> 部分定义的依赖项,其 version、scope 和其它属性会被所有子模块所继承。这意味着,当各个子模块需要引用相同 groupId 和 artifactId 的依赖时,它们无需明确指定版本号。Maven 会自动查找并使用在父级 POM 中<dependencyManagement> 部分声明的对应版本。
  2. 依赖范围和排除管理: 不仅可以管理依赖版本,还可以在此处集中管理依赖的 scope(例如 compile、test 等)以及排除特定的传递依赖。这样,在子模块中引入依赖时,相关的 scope 和 exclusion 规则也会得到继承和应用。
  3. 简化项目结构: 通过 <dependencyManagement>,项目可以避免在多个子模块之间重复配置相同的依赖信息,从而保持 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.aleks</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>deploy</module> <module>module</module> </modules> <properties> <java.version>1.8</java.version> <!-- 修改版本号 --> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.7.18</version> <!-- 同步版本号 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.18</version> <!-- 同步版本号 --> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.8.2</version> <!-- Java 8兼容版本 --> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <!-- Log4j2 API --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.20.0</version> </dependency> <!-- Log4j2 Core 实现 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.20.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <!-- java8最新稳定版 --> <scope>provided</scope> <!-- 必须设置为provided --> </dependency> </dependencies> </dependencyManagement> </project>帮我按标准排排序 备注加一下 规范一下
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值