Maven dependency exclusion in shaded jar

2.4k views Asked by At

So, I have a multi-module project that I create a shaded jar for to add to non-maven projects as well as to support other maven projects. We have some of our own libraries as dependencies. However, bundling them in the shaded jar is causing issues in the non-maven application that already has those dependencies (and cannot be just removed to rely on the shaded version). I've been trying to make a profile in my project's pom to exclude these dependencies from the shaded jar but maven doesn't seem to care.

I can't post the entire pom but in essence, each module depends on other modules, and at the end we have a final module that just bundles everything together which is where my profile resides. I've added a list of dependencies that all have exclusions for the artifacts I don't want added to the shaded jar, I've used dependency:tree to add every module that has a transitive dependency to the exclusion and added a dependency entry for it along with its own exclusion, I've added an artifactSet to the shade plugin configuration to exclude those artifacts, and I've added the enforce plugin to fail the build if that artifact exists.

The artifact I don't want added is still being added to the shaded jar.

<dependencies>
    <dependency>
        <groupId>my.group</groupId>
        <artifactId>my-module</artifactId>
        <version>1.2.3</version>
        <exclusions>
            <exclusion>
                <groupId>some.dependency</groupId>
                <artifactId>i-dont-want</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

This works, but the problem is that I need to also have a dependency for a module further down the dependency tree that transitively depends on the above module.

So, when I do the following, the dependency I'm trying to exclude ends up in the shaded jar anyway.

<dependencies>
    <dependency>
        <groupId>my.group</groupId>
        <artifactId>my-module</artifactId>
        <version>1.2.3</version>
        <exclusions>
            <exclusion>
                <groupId>some.dependency</groupId>
                <artifactId>i-dont-want</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>my.group</groupId>
        <artifactId>dependes-on-my-module</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>

If I try to add exclusions in my shade config as below, it doesn't seem to care, either.

<configuration>
    <artifactSet>
        <excludes>
            <exclude>some.dependency:i-dont-want</exclude>
        </excludes>
    </artifactSet>
    ...
</configuration>

I've tried searching online and have probably tried a ton of stuff on stack overflow that hasn't seemed to help (even marking things as provided in my profile or adding all the dependencies that have a transitive dependency to the artifact I'm trying to remove with no luck).

Is there any way I can force these transitive dependencies to not be added to my shaded jar? I just want to not have them added; at this point I'll try anything... thanks for help in advance.

1

There are 1 answers

0
Stephen On

Ended up being a dev adding a shade plugin in another module that ended up getting the transitive dependency included in itself and passed along. Aside from maven not seeming to have a way to just nuke something from orbit it seems to be working now.