maven配置相关dependencies与dependencyManagement

1、dependencies与dependencyManagement的区别

dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
base-parent(pom.xml)

    <packaging>pom</packaging>
    <groupId>com.java.test</groupId>
    <artifactId>base-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <description>desc</description>
<dependencies>  
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.34</version>
        </dependency>         
</dependencies>  

子项目pom ,怎子项目会继承所有父项目依赖

    <parent>
        <groupId>com.java.test</groupId>
        <artifactId>base-parent</artifactId>
        <version>1.0.4-SNAPSHOT</version>
    </parent>

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

父pom

   <dependencyManagement>    
        <dependencies>  
            <dependency>  
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.34</version>
                <scope>provided</scope>  
            </dependency> 
        </dependencies>  
    </dependencyManagement>

子项目

    <parent>
        <groupId>com.java.test</groupId>
        <artifactId>base-parent</artifactId>
        <version>1.0.4-SNAPSHOT</version>
    </parent>
<!-- 指明需要从父pom继承的依赖 -->
  <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>fastjson</artifactId>  
   </dependency>  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容