zoukankan      html  css  js  c++  java
  • Maven的dependency和dependencyManagement的区别

     

    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,然后在各个项目中导入这些依赖管理配置

  • 相关阅读:
    Mysql_大字段问题Row size too large.....not counting BLOBs, is 8126.
    Pycharm快捷键设置(鼠标滚动控制字体大小)
    python实现将base64编码的图片下载到本地
    [CentOS_7.4]Linux编译安装ffmpeg
    contenOs7
    文本特征提取方法研究
    Mahout中相似度计算方法介绍
    基于Mahout的电影推荐系统
    向Python女神推荐这些年我追过的经典书籍
    Mahout推荐算法API详解
  • 原文地址:https://www.cnblogs.com/shoshana-kong/p/14192684.html
Copyright © 2011-2022 走看看