maven dependencyManagement的作用

从Maven的继承开始说起:

假设有两个子模块sub-1和sub-2,其pom文件分别如下所示:


  4.0.0

  cn.codecrazy.demo
  demo-sub1
  1.0-SNAPSHOT
  jar

  
    
      org.springframework
      spring-context
      4.3.4.RELEASE
    
    
      org.springframework
      spring-beans
      4.3.4.RELEASE
    
    
      com.alibaba
      fastjson
      1.2.31
    
    
      junit
      junit
      3.8.1
      test
    
  


  4.0.0

  cn.codecrazy.demo
  demo-sub2
  1.0-SNAPSHOT
  jar

  
    
      org.springframework
      spring-context
      4.3.4.RELEASE
    
    
      org.springframework
      spring-beans
      4.3.4.RELEASE
    
    
      junit
      junit
      3.8.1
      test
    
  

可以看到sub1和sub2中都引入了junit、spring-context和spring-beans依赖。并且sub1中还另外引入了fastjson依赖。我们可以利用maven的继承机制,将这些依赖放到parent父模块中,并在子模块中引入节点。

在sub1和sub2的上层目录中新增一个parent模块,其pom文件如下所示:


  4.0.0

  cn.codecrazy.demo
  demo-parent
  1.0-SNAPSHOT
  pom

  
    
      org.springframework
      spring-context
      4.3.4.RELEASE
    
    
      org.springframework
      spring-beans
      4.3.4.RELEASE
    
    
      com.alibaba
      fastjson
      1.2.31
    
    
      junit
      junit
      3.8.1
      test
    
  

注意节点为pom方式,之后的sub1和sub2的pom文件可简化如下所示:


  4.0.0

  
    cn.codecrazy.demo
    demo-parent
    1.0-SNAPSHOT
    ../pom.xml
  

  demo-sub1


 
  4.0.0

  
    cn.codecrazy.demo
    demo-parent
    1.0-SNAPSHOT
    ../pom.xml
  

  demo-sub2


可以看到sub1和sub2模块的pom文件配置大大简化了,由于配置了parent节点,子模块的依赖从父模块得到了继承,即使在子模块中不配置节点,子模块也引入了它的依赖。

但是上述配置存在一个问题,sub2中并不需要fastjson依赖,但由于继承,也同样从父模块中继承了fastjson依赖。换句话说,以后继承自该parent模块的其他子模块也许并不需要parent模块中声明的所有依赖,但是就目前这种配置方式,父模块中的这些依赖都会被无条件的继承,这显然是不合理的,这就需要用到节点。

节点中配置的依赖并不会真正的引入依赖,但是该节点却能够被子模块继承,要在子模块中真正引入依赖,需要将依赖配置在节点下,而这种方式配置的依赖可以省略节点,使用的即是在节点中配置的相应。采用了节点的父模块pom文件如下:


  4.0.0

  cn.codecrazy.demo
  demo-parent
  1.0-SNAPSHOT
  pom
  
    
      
        org.springframework
        spring-context
        4.3.4.RELEASE
      
      
        org.springframework
        spring-beans
        4.3.4.RELEASE
      
      
        com.alibaba
        fastjson
        1.2.31
      
      
        junit
        junit
        3.8.1
        test
      
    
  

sub1和sub2模块pom文件相应如下所示:


  4.0.0

  
    cn.codecrazy.demo
    demo-parent
    1.0-SNAPSHOT
    ../pom.xml
  

  demo-sub1

  
    
      org.springframework
      spring-context
    
    
      org.springframework
      spring-beans
    
    
      com.alibaba
      fastjson
    
    
      junit
      junit
      test
    
  


  4.0.0

  
    cn.codecrazy.demo
    demo-parent
    1.0-SNAPSHOT
    ../pom.xml
  

  demo-sub2

  
    
      org.springframework
      spring-context
    
    
      org.springframework
      spring-beans
    
    
      junit
      junit
      test
    
  

可以看到在sub2的pom文件中没有引入fastjson依赖,这样sub2就不会真正的引入fastjson依赖。

节点的另一个好处是,使得多个子模块中引入的相同依赖版本号能够保持一致。

如何引入另外的dependencyManagement?

假设在某个模块demo-other中也有节点管理着一批依赖,现在想在demo-parent模块中将那些依赖管理全部导入,把demo-other模块中的依赖全部复制到demo-parent模块的节点下是可行但是非常不优雅的做法。

比较优雅的做法是用到import依赖范围,即import。demo-parent的pom文件如下所示:


  4.0.0

  cn.codecrazy.demo
  demo-parent
  1.0-SNAPSHOT
  pom
  
    
      
        cn.codecrazy.demo
        demo-other
        1.0-SNAPSHOT
        pom
        import
      
      
        org.springframework
        spring-context
        4.3.4.RELEASE
      
      
        org.springframework
        spring-beans
        4.3.4.RELEASE
      
      
        com.alibaba
        fastjson
        1.2.31
      
      
        junit
        junit
        3.8.1
        test
      
    
  

注意多出来的部分:


    cn.codecrazy.demo
    demo-other
    1.0-SNAPSHOT
    pom
    import

你可能感兴趣的:(maven dependencyManagement的作用)