Satyendra
Satyendra

Reputation: 1705

Stop maven dependency from getting further inherited to children

Say we have the following dependency hierarchy in maven:

 +- projectA
 |  +- projectB
 |  |  +- projectC
 |  |  +- projectD

Here projectB needs certain libraries from projectA (let's say libX). But, those libraries are not needed any further in the hierarchy (ie. C and D don't need libX). Now, as projectC and projectE are inheriting all the dependencies from their parent project (projectA), so they can provide for such dependency.

Q. Can we restrict the dependency inheritance at the level of projectB itself, so that we need not exclude it manually everywhere (viz. projectC, projectD, etc.)

Upvotes: 0

Views: 298

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35785

You can set <optional>true</optional> on the dependency in B.

Please note that if C calls a method of B that needs libX, this will then fail.

Upvotes: 1

C&#233;dric Couralet
C&#233;dric Couralet

Reputation: 5223

You can try using optional dependencies in projectB, this is the exact use-case :

https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

In your pom something like :

    <project>
      ...
      <dependencies>
        <!-- declare the dependency to be set as optional -->
        <dependency>
          <groupId>sample.ProjectA</groupId>
          <artifactId>ProjectA</artifactId>
          <version>1.0</version>
          <scope>compile</scope>
          <optional>true</optional> <!-- value will be true or false only -->
        </dependency>
      </dependencies>
    </project>

Upvotes: 1

Related Questions