mavenpom.xmldependency-managementtransitive-dependency

How to force maven dependency version via dependencyManagement in parent pom?


Let's say B:1.0.1 has transitive dependency A:1.0.1, but the child project is supposed to depend on A:1.0.2 (with intentional overriding transitive dependencies).

It is easy to discover that the order of dependencies in <dependencyManagement> affect versions overriding, so adding A:1.0.2 in the child pom just before B:1.0.1 would force using A:1.0.2 even as a dependency for B:1.0.1.

In this case I'm looking for a way to declare A:1.0.2 in the parent pom, and remove boilerplate from all its children. Unfortunately, the following setup leads to using both versions in the final artifact: A:1.0.1 (comes as a dependency of B:1.0.1) and A:1.0.2 (comes from the explicit declaration in the parent pom).

How to force using version A:1.0.2 in all child projects, keeping the declaration in the parent?

Parent pom:

<groupId>my-group</groupId>
<artifactId>my-parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<dependencyManagement>
  <dependencies>   
    <dependency>
      <groupId>g</groupId>
      <artifactId>A</artifactId>
      <version>1.0.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Child pom:

<parent>
  <groupId>my-group</groupId>
  <artifactId>my-parent</artifactId>
  <version>0.0.1</version>
</parent>

<artifactId>my-child</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<dependencies>   
  <dependency>
    <groupId>g</groupId>
    <artifactId>A</artifactId>
    <!-- version 1.0.2 comes from the parent pom -->
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>   
    <dependency>
      <groupId>g</groupId>
      <artifactId>B</artifactId>
      <version>1.0.1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Solution

  • You are using the dependencyManagement incorrectly.

    If A and B are jar artifacts, you should not have the tags

    <type>pom</type>
    <scope>import</scope>
    

    These are for BOMs only.