electrotype
electrotype

Reputation: 8816

maven-shade-plugin: exclude a dependency and all its transitive dependencies

Using maven-shade-plugin, is there a way to exclude a dependency (which is not provided) and all its transitive dependencies?

For example :

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>some-artifact</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>

    ... other dependencies

</dependencies>

and 1)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <artifactSet>
                    <includes>
                        <include>*:*</include>
                    </includes>
                    <excludes>
                        <exclude>com.example:some-artifact</exclude>
                    </excludes>
                </artifactSet>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

or 2)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <artifactSet>
                    <includes>
                        <include>*:*</include>
                    </includes>
                </artifactSet>
                <filters>
                    <filter>
                        <artifact>com.example:some-artifact</artifact>
                        <excludes>
                            <exclude>**</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Those don't work.

All the transitive dependencies of com.example:some-artifact are added to the final jar.

Note that I don't want to set the scope of com.example:some-artifact to provided.

Upvotes: 13

Views: 23020

Answers (3)

user12517532
user12517532

Reputation: 21

You must take in mind that by default all dependencies COMPILE will be included. But if you set artifacts in artifactSet includes, only those will be considered and the rest will be excluded (dependencies and its transitive dependencies)

Sometimes it's easier include only the dependencies you need than exclude all the rest.

Upvotes: 1

catch32
catch32

Reputation: 18622

I tried following configuration, and it worked for me also:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <finalName>client-${artifactId}</finalName>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*</exclude>
                </excludes>
            </filter>
        </filters>
        <artifactSet>
            <excludes>
                <exclude>org.apache.jmeter:*</exclude>
                <exclude>com.fasterxml.jackson.core:jackson-databind:*</exclude>
                <exclude>com.fasterxml.jackson.module:jackson-module-scala_2.11:*</exclude>
            </excludes>
        </artifactSet>
    </configuration>
</plugin>

Upvotes: 11

dcsohl
dcsohl

Reputation: 7406

Run "shade" from within a profile, and mark your dependency as provided only in that profile. For example:

<profiles>
    <profile>
        <id>shadeProfile</id>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>some-artifact</artifactId>
                <version>1.23</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <shadedClassifierName>shaded</shadedClassifierName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

When you run mvn -PshadeProfile package it will treat your dependency as provided (and thus omit its dependencies), and it will use the classifier "shaded" so you can use this as a dependency in other modules.

Upvotes: 14

Related Questions