Maven Shade Plugin and Missing Transitive Dependencies

Transitive Dependencies

It’s widely known that when adding a dependency to a Maven project, its own dependencies are added automatically. This is Maven’s transitive dependency mechanism.

After Adding Shade Plugin

Adding Shade plugin to pom.xml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>fat</shadedClassifierName>
            </configuration>
        </execution>
    </executions>
</plugin>

Then a fat jar will be generated to the target dir, along with the original jar.

However, after mvn install, user can’t find this library’s transitive dependencies:

1
2
3
4
$ mvn dependency:tree
<... omitted ...>
+- com.example:artifact:jar:a.b.c:compile
<... omitted ...>

Just one line of the library itself.

Debug and Fix

Look back into library’s source, a file named dependency‑reduced‑pom.xml was generated by Shade plugin. This POM is generated in order ro avoid bundling the shaded dependencies again. So mvn install uses dependency‑reduced‑pom.xml as POM.

A difference between the original pom.xml and the generated dependency‑reduced‑pom.xml is that <scope>provided</scope> is added to dependencies in dependency‑reduced‑pom.xml. That’s why transitive dependencies are missing.

The fix is easy, just disable the reduced pom:

1
2
3
<configuration>
    <createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>

and delete the generated dependency‑reduced‑pom.xml in the source.

Licensed under CC BY-NC-SA 4.0
"Decouple with me!" -- said Java.