两个应用程序中的 jar 冲突 - maven-shade-plugin 重定位

jar conflict in two applications - maven-shade-plugin relocation

我需要有关 maven-shade-plugin 的帮助。 我有一个应用程序 A,它计划添加应用程序 B 的客户端。问题是,应用程序 A 和应用程序 B 都使用一个库(我们称之为冲突库)但版本不同。我无法更改任何这些应用程序中的版本,因为它们依赖于不同版本的 Jersey。 有人告诉我 maven-shade-plugin 应该能够解决这个问题。 我查阅了一些示例并阅读了文档,但我并不完全了解它是如何工作的。

我在我的 pom.xml 应用程序 "B" 中添加了以下代码段。 我要在应用程序 A 中添加应用程序 B 依赖项。我需要添加转换器吗?有什么需要添加/排除的吗?提前致谢。

<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>
                <configuration>
                    <minimizeJar>true</minimizeJar>
                    <relocations>
                        <relocation>
                            <pattern>com.conflict.lib</pattern>
               <shadedPattern>shaded.com.conflict.lib</shadedPattern>
                        </relocation>
                    </relocations>
                </configuration>
            </execution>
        </executions>
</plugin>

这将如何运作?当我 运行 mvn 安装时,我在控制台中看到下面

Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/.../application-b-client/target/application-b-client-1.0.2-SNAPSHOT.jar with /Users/.../application-b-client/target/application-b-client-1.0.2-SNAPSHOT-**shaded**.jar
[INFO] Dependency-reduced POM written at: /Users/.../application-b-client/dependency-reduced-pom.xml

这个阴影工件不在我的 .m2 文件夹中。它就在我的项目目标文件夹中。我应该在 Application "A" 的 pom.xml 中输入什么? 目前我只有:

    <dependency>
        <groupId>com.my</groupId>
        <artifactId>application-b-client</artifactId>
        <version>1.0.2-SNAPSHOT</version>
    </dependency>

我应该用 1.0.2-SNAPSHOT.shaded 版本替换它吗?

乍一看,这对我来说是正确的。

带阴影的 .jar 替换了 原始的 .jar - 所以不要指望在您的存储库中找到两者。

如果一切按预期进行,application-b-client-1.0.2-SNAPSHOT.jar 文件中应该有一个名为 'shaded' 的包,其中包含重定位的包。不是这样吗?

编辑:

this client jar is being used in other projects too, but non-shaded version

如果您(或客户)以后需要原始和阴影 .jars,您可以添加以下配置:

<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>

这不是替换原件,而是使阴影 .jar 附加 到原件。

然后您可以使用以下任一方式要求工件:

<!-- This is the original non-shaded artifact -->
<dependency>
    <groupId>com.my</groupId>
    <artifactId>application-b-client</artifactId>
    <version>1.0.2-SNAPSHOT</version>
</dependency>

-或-

<!-- This is the shaded artifact -->
<dependency>
    <groupId>com.my</groupId>
    <artifactId>application-b-client</artifactId>
    <version>1.0.2-SNAPSHOT</version>
    <classifier>shaded</classifier>
</dependency>