Maven的dependency和dependencyManagement的区别

本文详细解释了Maven中dependencies和dependencyManagement的区别及使用场景,通过实例展示了如何配置父项目来管理子项目的依赖版本,以及如何利用import范围来导入其他项目的依赖管理。

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



dependencies:

就算在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)

dependencyManagement:

只是声明依赖,并不实现引入,因此子项目需要显示声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。


例子:

父项目pom.xml

<properties>
	<springframework.version>2.5.6</springframework.version>
	<junit.version>4.7</junit.version>
</properties>
<dependencyManagement>  
          
        <dependencies>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>  
                <version>${springframework.version}</version>
            </dependency>  
              
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-beans</artifactId>  
                <version>${springframework.version}</version>  
            </dependency>  

            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context</artifactId>  
                <version>${springframework.version}</version>  
            </dependency>  
        </dependencies>  
    </dependencyManagement>  


这里使用dependencyManagement声明的依赖既不会给父项目引入依赖,也不会给他的子模块引入依赖,不过这段配置是会被继承

用到父项目的依赖的子项目pom.xml配置


<dependencies>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-core</artifactId>
    </dependency>  
      
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>
    </dependency>  

    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>
    </dependency>  
</dependencies>  


不需要父项目的依赖的子项目pom.xml配置,只需要引入父项目的依赖即可

<dependencies>  
    <dependency>  
        <groupId>dom4j</groupId>  
        <artifactId>dom4j</artifactId>
	<version>${dom4j.version}</version>
    </dependency>  
      
    <dependency>  
        <groupId>junit</groupId>  
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>  


这里没有声明父项目的依赖,那么该依赖就不会被引入。这正是dependencyManagement的灵活性所在。


import依赖范围

import范围只有在denpendencyManagement元素下才有效果

如果你想要把项目A项目的依赖用于另外一个项目就需要使用import范围将这配置导入

<dependencyManagement>  
          
        <dependencies>  
            <dependency>  
                <groupId>com.mvnbook.account</groupId>  
                <artifactId>account-parent</artifactId>  
                <version>1.0-SNAPSHOT</version>
		<type>pom</type>
		<scope>import</scope>
            </dependency>  
        </dependencies>  
    </dependencyManagement>  


上述代码中type的值为pom,import范围由于其特殊性,一般都是指向打包类型为pom的模块。如果有多个先忙,他们使用的版本都是一致的,则就可以定义一个使用

dependencyManagement专门管理依赖的POM,然后在各个项目中导入这些依赖管理配置


### MavendependencyManagement 的用法配置 #### 使用场景 当项目模块众多时,使用 Maven 管理项目变得尤为便捷。这不仅有助于管理构建、文档、报告、依赖关系等方面的工作,还能确保所有子项目使用的依赖项及其版本保持一致,从而保障测试环境与生产环境中的一致性[^1]。 #### 配置方式 在顶层 POM 文件中定义 `dependencyManagement` 元素用于集中管理声明依赖的版本号。这种方式允许开发者在一个地方设定好所需 jar 包的具体版本信息,使得各子项目只需简单引用这些依赖而不必重复指定其版本号。具体来说: - **父级 POM**: 定义全局性的依赖管理规则。 ```xml <project> ... <dependencyManagement> <dependencies> <!-- 示例:Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 更多依赖... --> </dependencies> </dependencyManagement> ... </project> ``` - **子级 POMs**: 继承自父级并利用已定义好的依赖设置 子项目的 pom.xml 可以像下面这样简洁地引入所需的依赖,无需再次指明版本号: ```xml <project> ... <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- 版本由父POM中的dependencyManagement控制 --> </dependency> <!-- 添加其他本地特有的依赖 --> </dependencies> ... </project> ``` 这种机制有效减少了跨多个子模块间可能出现的版本不匹配问题,并简化了整个项目的维护工作量[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值