Differences between dependencyManagement and dependencies in Maven

前端 未结 13 1395
北荒
北荒 2020-11-22 04:14

What is the difference between dependencyManagement and dependencies? I have seen the docs at Apache Maven web site. It seems that a dependency def

13条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 04:58

    The documentation on the Maven site is horrible. What dependencyManagement does is simply move your dependency definitions (version, exclusions, etc) up to the parent pom, then in the child poms you just have to put the groupId and artifactId. That's it (except for parent pom chaining and the like, but that's not really complicated either - dependencyManagement wins out over dependencies at the parent level - but if have a question about that or imports, the Maven documentation is a little better).

    After reading all of the 'a', 'b', 'c' garbage on the Maven site and getting confused, I re-wrote their example. So if you had 2 projects (proj1 and proj2) which share a common dependency (betaShared) you could move that dependency up to the parent pom. While you are at it, you can also move up any other dependencies (alpha and charlie) but only if it makes sense for your project. So for the situation outlined in the prior sentences, here is the solution with dependencyManagement in the parent pom:

    
    
      
        
           
            alpha
            alpha
            1.0
            
              
                zebra
                zebra
              
            
          
          
            charlie 
            charlie
            1.0
            war
            runtime
          
           
            betaShared
            betaShared
            1.0
            bar
            runtime
          
        
      
    
    
    
    
      
        
          alpha
          alpha  
        
        
          betaShared
          betaShared
          bar 
        
      
    
    
    
    
      
        
          charlie
          charlie
          war 
        
        
          betaShared 
          betaShared 
          bar 
        
      
    
    

提交回复
热议问题