5

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.

Vapid
  • 701
  • 7
  • 27
  • You could try to exclude your jar... – dan1st Aug 30 '20 at 17:31
  • I guess I will give that a try, but I don't understand how excluding my own jar (which is the jar I am compiling) from itself would help... @dan1st – Vapid Aug 31 '20 at 06:50
  • 1
    It seems the problem is that maven tries to include your old jar when building a new jar. This may be because you included `*.*` and `murder-1.0-SNAPSHOT.jar` matches `*.*`. – dan1st Aug 31 '20 at 06:51
  • I tried to exclude my own files using a `filter`, but that entirely excluded all my files from the jar. So the jar was just full of my dependencies but not my own classes haha... – Vapid Sep 14 '20 at 17:09
  • It should be sufficient to exclude your own, autogenerated jar. – dan1st Sep 14 '20 at 17:37

1 Answers1

-1

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>
Vapid
  • 701
  • 7
  • 27