lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1846 随笔 :: 0 文章 :: 109 评论 :: 288万 阅读

 

dependencies:

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

dependencyManagement:

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

 

例子:

父项目pom.xml

  1.  
    <properties>
  2.  
    <springframework.version>2.5.6</springframework.version>
  3.  
    <junit.version>4.7</junit.version>
  4.  
    </properties>
  5.  
    <dependencyManagement>
  6.  
     
  7.  
    <dependencies>
  8.  
    <dependency>
  9.  
    <groupId>org.springframework</groupId>
  10.  
    <artifactId>spring-core</artifactId>
  11.  
    <version>${springframework.version}</version>
  12.  
    </dependency>
  13.  
     
  14.  
    <dependency>
  15.  
    <groupId>org.springframework</groupId>
  16.  
    <artifactId>spring-beans</artifactId>
  17.  
    <version>${springframework.version}</version>
  18.  
    </dependency>
  19.  
     
  20.  
    <dependency>
  21.  
    <groupId>org.springframework</groupId>
  22.  
    <artifactId>spring-context</artifactId>
  23.  
    <version>${springframework.version}</version>
  24.  
    </dependency>
  25.  
    </dependencies>
  26.  
    </dependencyManagement>


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

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

 

  1.  
    <dependencies>
  2.  
    <dependency>
  3.  
    <groupId>org.springframework</groupId>
  4.  
    <artifactId>spring-core</artifactId>
  5.  
    </dependency>
  6.  
     
  7.  
    <dependency>
  8.  
    <groupId>org.springframework</groupId>
  9.  
    <artifactId>spring-beans</artifactId>
  10.  
    </dependency>
  11.  
     
  12.  
    <dependency>
  13.  
    <groupId>org.springframework</groupId>
  14.  
    <artifactId>spring-context</artifactId>
  15.  
    </dependency>
  16.  
    </dependencies>


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

  1.  
    <dependencies>
  2.  
    <dependency>
  3.  
    <groupId>dom4j</groupId>
  4.  
    <artifactId>dom4j</artifactId>
  5.  
    <version>${dom4j.version}</version>
  6.  
    </dependency>
  7.  
     
  8.  
    <dependency>
  9.  
    <groupId>junit</groupId>
  10.  
    <artifactId>junit</artifactId>
  11.  
    </dependency>
  12.  
    </dependencies>

 

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


import依赖范围

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

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

  1.  
    <dependencyManagement>
  2.  
     
  3.  
    <dependencies>
  4.  
    <dependency>
  5.  
    <groupId>com.mvnbook.account</groupId>
  6.  
    <artifactId>account-parent</artifactId>
  7.  
    <version>1.0-SNAPSHOT</version>
  8.  
    <type>pom</type>
  9.  
    <scope>import</scope>
  10.  
    </dependency>
  11.  
    </dependencies>
  12.  
    </dependencyManagement>


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

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

posted on   白露~  阅读(162)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2018-12-26 java 字符串和集合互相转换
点击右上角即可分享
微信分享提示