maven-shade-plugin:排除依赖关系及其所有​​传递依赖关系

时间:2015-02-11 15:26:42

标签: java maven maven-shade-plugin

使用maven-shade-plugin,有没有办法排除依赖关系(不是"提供")及其所有传递依赖

例如:

<dependencies>

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

    ... other dependencies

</dependencies>

和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>

或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>

那些不能工作的人。 com.example:some-artifact的所有传递依赖都被添加到最终的jar中。请注意,我不想将com.example:some-artifact的范围设置为&#34;提供&#34;。

3 个答案:

答案 0 :(得分:6)

运行&#34;阴影&#34;在配置文件中,并将您的依赖关系标记为仅在该配置文件中提供。例如:

<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>

当你运行mvn -PshadeProfile package时,它将按照提供的方式处理你的依赖关系(从而省略它的依赖关系),它将使用分类器&#34; shaded&#34;所以你可以把它作为其他模块的依赖。

答案 1 :(得分:3)

我尝试了以下配置,它也适用于我:

        <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>

答案 2 :(得分:-1)

您必须记住,默认情况下将包含所有依赖关系COMPILE。但是,如果您在artifactSet的includes中设置了工件,则只会考虑那些工件,而其余的工件将被排除(依赖关系及其传递性依赖关系)

有时候,只包含所需的依赖项比排除所有其余的要容易。