java maven-shade-plugin :排除依赖项及其所有传递依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28458058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:37:52  来源:igfitidea点击:

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

javamavenmaven-shade-plugin

提问by electrotype

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

使用maven-shade-plugin, 有没有办法排除依赖项(未“提供”)及其所有传递依赖项

For example :

例如 :

<dependencies>

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

    ... other dependencies

</dependencies>

and 1)

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

或 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-artifactare added to the final jar. Note that I don't want to set the scope of com.example:some-artifactto "provided".

那些不起作用。的所有传递依赖项com.example:some-artifact都添加到最终的 jar 中。请注意,我不想将范围设置com.example:some-artifact为“提供”。

回答by dcsohl

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

从配置文件中运行“shade”,并将您的依赖项标记为仅在该配置文件中提供。例如:

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

当您运行时,mvn -PshadeProfile package它会将您的依赖项视为提供的(因此省略其依赖项),并且它将使用分类器“阴影”,因此您可以将其用作其他模块中的依赖项。

回答by nazar_art

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.Hymanson.core:Hymanson-databind:*</exclude>
                <exclude>com.fasterxml.Hymanson.module:Hymanson-module-scala_2.11:*</exclude>
            </excludes>
        </artifactSet>
    </configuration>
</plugin>

回答by user12517532

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)

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

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

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