Maven仅包含阴影罐中的依赖性 [英] Maven Include Dependency in Shaded Jar Only

查看:90
本文介绍了Maven仅包含阴影罐中的依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用maven shade来创建一个带阴影的jar,但我还想在shaded jar的情况下包含一个特殊的依赖项(不在我项目的正常编译依赖项中)。我怎么能这样做?

I'd like to use maven shade to create a shaded jar, but I'd also like to include a special dependency only in the case of the shaded jar (not in the normal compile dependencies of my project). How can I go about this?

根据我的理解,包含/排除只是白名单/黑名单所以我不能明确强制要包含的内容不包括在内在实际依赖列表中。

From my understanding the includes/excludes are only whitelists/blacklists so I can't explicitly force something to get included that wasn't included in the actual dependency list.

对于更多上下文,我有一个JAR依赖项,其中包含一个我只希望包含在我的一个阴影工件中的资源,但是有这个jar在类路径上否则会导致错误。

For more context, I have a JAR dependency which contains a resource that I only want included in one of my shade artifacts, but having that jar on the classpath otherwise would cause errors.

为了清楚起见,我希望生成一个带阴影的jar,带有额外的依赖项和没有它的普通jar,一个 mvn包调用。

To be clear, I'm looking to generate both a shaded jar, with an additional dependency and a normal jar without it, in one mvn package call.

推荐答案

尝试使用个人资料,并拥有您的依赖关系和阴影仅在该配置文件中。例如:

Try using a profile, and having your dependency and shade 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>
            </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包时它将神器作为着色构建的一部分包含在内,并使用分类器着色作为新工件。这样,您的构建可以在没有有问题的资源的情况下生成无阴影的JAR,并且只需打开配置文件就可以生成带有该资源的着色JAR。

Now, when you run mvn -PshadeProfile package it will include the artifact as part of your shaded build, and use the classifier shaded for the new artifact. This way your build can produce your unshaded JAR without the problematic resource, and a shaded JAR with that resource, simply by turning on the profile.

依赖于此的其他项目可以选择依赖于着色或无阴影的工件,因为您使用分类器来生成两者。

Other projects which depend on this can either depend on the shaded or unshaded artifact, as appropriate, since you are using a classifier to generate both.

这篇关于Maven仅包含阴影罐中的依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆