Kunal gupta
Kunal gupta

Reputation: 581

Exclude transitive dependency maven

I have a project where I need to use a 3rd party library X. X internally has dependency on Y but I don't need Y in my project since it causes some issues.

How can I exclude Y from my project but still internally let X use it.

pomX.xml

    <dependency>
      <groupId>Y</groupId>
      <artifactId>Y</artifactId>
      <version>Y.version</version> 
    </dependency>

projectPom.xml

   <dependency>
      <groupId>X</groupId>
      <artifactId>X</artifactId>
      <version>X.version</version> 
   </dependency>

What i tried was this-

ProjectPom.xml

 <dependency>
      <groupId>X</groupId>
      <artifactId>X</artifactId>
      <version>X.version</version> 
     <exclusions>
          <exclusion>
              <groupId>Y</groupId>
              <artifactId>Y</artifactId>
          </exclusion>
     </exclusions>
 </dependency>

But this causes Y to be not available for X too during the build. How can I exclude Y from my project but still let X use it internally?

Upvotes: 1

Views: 1468

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35785

Sorry, this is not possible.

Maven takes the dependencies and constructs classpaths.

If Y is on the classpath, then it is available to all other elements of the classpath, no matter where it originally came from.

You can make distinctions like "I need this at compile time, not at runtime" or vice versa, but you cannot restrict dependency Y to usage by X.

Upvotes: 1

slartidan
slartidan

Reputation: 21566

Exclude it as transitive dependency (exclusion) and at the same time add it as your own dependency using provided.

<dependencies>
    <dependency>
        <groupId>X</groupId>
        <artifactId>X</artifactId>
        <version>X.version</version> 
        <exclusions>
            <exclusion>
                <groupId>Y</groupId>
                <artifactId>Y</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>Y</groupId>
        <artifactId>Y</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

This way X will be part of your build result (i.e. when building a war-file), but Y will not. Still Y is available at compile time.

Upvotes: 0

Related Questions