0

I have gone through differences between dependencymanagement and dependencies in maven but i am still unclear when to use just dependencies tag in parent pom and when to use dependenciesManagement tag ?

My understanding is when my all child modules need to use same dependency version then we should declare the Dependencies under Dependencies tag(without dependencyManagement tag)

But on other hand if some of the child project need to use different version then we should declare the Dependencies under Dependencies tag(which will be under dependencyManagement tag). then Child modules can refer them with overridden version

Is that correct ?

Community
  • 1
  • 1
emilly
  • 10,060
  • 33
  • 97
  • 172
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven – Clement Amarnath Apr 21 '16 at 10:40

1 Answers1

2

Declaring a <dependency> within <dependencyManagement> does not set the specified artifact as dependency for any project – parent or childs. It just states: If you want to use this as dependency then you can use it with these settings (version, scope, ...) without having to specify the settings again, and again, and ... You can, however, override a "management" setting in a "real" <dependency> anytime.

See also POM Reference, Dependency Management.

There are two options for a parent POM regarding your second paragraph:

  1. As you describe correctly:

    <dependencies>
      <dependency>
        <groupId>g-id</groupId>
        <artifactId>a-id</artifactId>
        <version>1.0.0</version>
      </dependency>
    </dependencies>
    
  2. I'd use this for consistency:

    <dependencyManagement>
      <dependencies>
          <dependency>
            <groupId>g-id</groupId>
            <artifactId>a-id</artifactId>
            <version>1.0.0</version>
          </dependency>
       </dependencies>
     </dependencyManagement>
    
    <dependencies>
      <dependency>
        <groupId>g-id</groupId>
        <artifactId>a-id</artifactId>
      </dependency>
    </dependencies>
    

Your third paragraph is correct..

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • Thanks Gerold. I understand usage of 'dependencyManagement', But can you see if my updated understanding about diff b/w `dependencies` and `dependencyManagement` is correct ? – emilly Apr 21 '16 at 11:12
  • As you said `.I'd use this for consistency:` i,e. approach 2 instead of 1 . My point is if I am sure that my all parent/child modules need to use same dependency version then should not I use approach 1 instead of 2 ? Because in approach 2, I have to unnecessarily mention the dependency in child module – emilly Apr 24 '16 at 09:43
  • Also can you elaborate 'A dependency setting that overrides an inherited one doesn't refer to it. It stands for itself' ? – emilly Apr 24 '16 at 09:45
  • @emilly Re "_I have to unnecessarily mention the dependency in child module_" – No, you don't have to: `g-id.a-id` is also declared as `/`. Such it will be inherited by childs (with the version declared in `///`). – Gerold Broser Apr 24 '16 at 10:27
  • @emilly Re "_can you elaboate_" – I meant that if you override a dependency inherited by a parent (completely, i.e. all settings) changes to that parent dependency's settings will not be reflected in the overriding dependency. But you're right. If one adds a setting to the parent it is also reflected at the child, so there obviously is a reference. I'm going to delete this sentences. Thanks! – Gerold Broser Apr 24 '16 at 10:27
  • Regarding your comment `No, you don't have to..` Agreed I don't have to here as` g-id.a-id is also declared as /. `. But again point is if I am sure that my all parent/child modules need to use same dependency version then why I would declare the `` under `` ? I can just declare it as `/` . Right ? – emilly Apr 24 '16 at 11:24
  • @emilly Yes, you can. The choice is yours. But, isn't it likely that there is a `` section (apart from very simple projects)? And if it's already there why not use it? Such being consistent with other managed dependency declarations. – Gerold Broser Apr 24 '16 at 11:37