Is it possible to shade a dependency?

1.3k views Asked by At

My project has dependencies lib-A and third-party lib-B:1.0 in my pom. But lib-A depends on lib-b:2.0. From my understanding, if lib-A had a shaded version of lib-b then that would solve the problem, correct? But the issue is lib-b is a third-party dependency which I have no control over.

Is there a work around so my project and lib-A will work correctly with different version of lib-b?

1

There are 1 answers

2
ravthiru On

Workaround is to shade lib-b with your project.

Edit :

Create new project say shaded-lib-b with lib-b as dependency and in your project you need have dependency for shaded-lib-b and now package name of lib-b will be my.shaded.example

pom.xml for shaded-lib-b

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

       <groupId>my.shaded.example</groupId>
        <artifactId>shaded-lib-b</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>lib-b</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

<build>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-shade-plugin</artifactId>
       <version>2.4.1</version>
       <executions>
         <execution>
           <phase>package</phase>
           <goals>
             <goal>shade</goal>
           </goals>
           <configuration>
             <relocations>
               <relocation>
                 <pattern>com.example</pattern>
                 <shadedPattern>my.shaded.example</shadedPattern>
               </relocation>
             </relocations>
             <transformers>
               <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
             </transformers>
           </configuration>
         </execution>
       </executions>
     </plugin>
   </plugins>
</build>
</project>