gpt4 book ai didi

maven - 使用maven-shade-plugin时出现依赖冲突怎么办?

转载 作者:行者123 更新时间:2023-12-01 19:36:10 43 4
gpt4 key购买 nike

我正在使用 maven-shade-plugin 创建一个可执行 jar,其中包含项目的所有依赖项。有时,这些依赖项会带来自己的依赖项,这些依赖项会与其他库的依赖项发生冲突,并且 maven-shade-plugin 会警告我不确定要在 uber jar 中包含哪个版本。

[WARNING] maven-shade-plugin has detected that some .class files
[WARNING] are present in two or more JARs. When this happens, only
[WARNING] one single version of the class is copied in the uberjar.
[WARNING] Usually this is not harmful and you can skeep these
[WARNING] warnings, otherwise try to manually exclude artifacts
[WARNING] based on mvn dependency:tree -Ddetail=true and the above
[WARNING] output

一般来说,我对此警告的回应是使用 <exclusions>我的 pom 文件中的依赖项声明元素,用于从我的项目中删除有问题的依赖项:

<!-- Amazon ElastiCache Client -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>elasticache-java-cluster-client</artifactId>
<version>1.0.61.0</version>
<exclusions>
<!-- this junit dependency clashes with our test-scoped one and causes integration tests to fail to run -->
<exclusion>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
</exclusion>
<!-- this dependency brings in two versions of cglib that clash with one another -->
<exclusion>
<groupId>jmock</groupId>
<artifactId>jmock-cglib</artifactId>
</exclusion>
<!-- newer versions of these dependencies come with dropwizard-core -->
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

当我这样做时,我使用 mvn dependency:tree确保我排除了有问题的依赖项的较低版本,希望最新版本是最成熟且没有错误的。

像上面这样的案例最终会被大量排除,这对这种做法提出了两个问题:

  1. 在上面的示例中,为什么我必须手动排除 junit 和 jmock?这两个依赖项都标记为 <scope>test</scope>the elasticache-java-cluster-client pom.xml ,所以我希望它们不会包含在我从 Maven 获得的 jar 中。
  2. 虽然到目前为止,我始终采用较新版本的依赖项的做法似乎很有效,但我担心有一天我会破坏某些东西。有没有更好的方法来确定要保留哪个版本的依赖项?

最佳答案

您是否尝试过添加 maven-enforcer-pluginDependencyConvergence rule ?这对我来说与阴影插件结合使用效果很好。它会告诉您哪些 Artifact 引入了同一类的不同版本。它让我知道我必须排除什么。

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<DependencyConvergence/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>

关于maven - 使用maven-shade-plugin时出现依赖冲突怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35319539/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com