Maven Shade overlapping classes warning

3.6k Views Asked by At

I am using commons-io in my project and want to shade it. I am running into a warning that I can't seem to figure out:

[WARNING] commons-io-2.7.jar, murder-1.0-SNAPSHOT.jar define 180 overlapping classes and resources:
...

I find this strange, as murder-1.0-SNAPSHOT.jar is the jar I am trying to build and which should include the commons-io jar.

I define my commons-io dependency as such:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.7</version>
    <scope>compile</scope>
</dependency>

(I thought I was supposed to use the runtime scope, but then I can't run package because it complains that FileUtils cannot be found)

Here's my config for the shade plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>

    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <artifactSet>
                    <includes>
                        <include>commons-io:commons-io</include>
                    </includes>
                </artifactSet>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/MANIFEST.MF</exclude>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

If I remove the <filters> entirely, I only get this warning:

commons-io-2.7.jar, murder-1.0-SNAPSHOT.jar define 1 overlapping resource
[WARNING]   - META-INF/MANIFEST.MF

Everything still seems to work fine, but I want to get rid of this warning when I package.

EDIT:

If I run mvn clean first, the next mvn package produces no such warning. Subsequent runs however introduces the warning again.

1

There are 1 best solutions below

1
On

In this end, this is what I ended up with that seems to work and produces no warnings:

<properties>
    <!-- replace this with wherever you want your shaded dependencies to end up -->
    <shaded-dependencies>io.vapidlinus.shade</shaded-dependencies>
</properties>
....
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <artifactSet>
                <includes>
                    <include>commons-io:commons-io</include>
                </includes>
            </artifactSet>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>module-info.class</exclude>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.MF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                        <exclude>META-INF/**</exclude>
                    </excludes>
                </filter>
            </filters>
            <relocations>
                <relocation>
                    <pattern>org.apache.commons.io</pattern>
                    <shadedPattern>${shaded-dependencies}.org.apache.commons.io</shadedPattern>
                </relocation>
            </relocations>
        </configuration>
    </execution>
</executions>