简体   繁体   English

我可以通过本地存储库覆盖Maven依赖版本吗?

[英]Can I overwrite a maven dependency version through my local repository?

I have a bit of a dependency hell situation here that I'm trying to resolve: I have three projects, A, B and C. A and B both depend on C. Now A is my own module that I have direct control over, B is a library I'm using, C is a library that is used both directly from my module A and by my dependency B. 我在这里尝试解决一个依赖地狱的情况:我有三个项目A,B和C。A和B都依赖C。现在A是我自己的模块,可以直接控制它, B是我正在使用的库,C是直接在我的模块A和依赖项B中使用的库。

To visualize this: 可视化:

   C
   ^
 /  B
|   ^
 \ /
  A

For various reasons I now needed to make a small change to C that I need locally but don't want to (or can't) deploy to the global repository from which C is downloaded normally. 由于各种原因,我现在需要对C进行一些小的更改,而我需要在本地进行一些更改,但又不想(或无法)将其部署到可以正常下载C的全局存储库中。

I tried to do this by tagging my modified version of C installed in my local repository with a classifier and changing the dependency to it in A's POM to include the classifier like this: 我试图通过使用分类器标记安装在本地存储库中的C的修改版本并在A的POM中更改对它的依赖项以包括这样的分类器来做到这一点:

<dependency>
  <groupId>foo</groupId>
  <artifactId>C</artifactId>
  <version>0.7.16</version>
  <classifier>myclassifier</classifier>
</dependency>

But mvn dependency:tree now shows that I have both the version with the classifier and the version without it in my classpath because of the transitive dependency: 但是mvn dependency:tree现在显示我的类路径中同时包含带分类器的版本和不带分类器的版本,这是由于可传递的依赖性:

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ A ---
[INFO] org.example:A:jar:0.1-SNAPSHOT
...
[INFO] +- foo:C:jar:myclassifier:0.7.16:compile
[INFO] +- bar:B:jar:3.2.5:compile
[INFO] |  +- foo:C:jar:0.7.16:compile

Is there any way I can force my project / maven to just use my modified version in this context? 有什么办法可以强制我的项目/ maven在这种情况下只使用修改后的版本?

Edit: Solution 编辑:解决方案

For now, I've solved it using exclusions like this in A's pom.xml thanks to the answers by Filipe and Samuel. 现在,由于Filipe和Samuel的回答,我已经使用A的pom.xml中的此类排除项解决了它。

<dependency>
  <groupId>foo</groupId>
  <artifactId>C</artifactId>
  <version>0.7.16</version>
  <classifier>myclassifier</classifier>
</dependency>

<dependency>
  <groupId>bar</groupId>
  <artifactId>B</artifactId>
  <version>3.2.5</version>
  <exclusions>
    <exclusion>
      <groupId>foo</groupId>
      <artifactId>C</artifactId>
    </exclusion>
  <exclusions>
</dependency>

Note: This of course only works as long as the edits in C are really minor and don't change the API used by B, which for me is the case. 注意:当然,这仅在C语言中的编辑确实很小且不更改B语言所使用的API的情况下才有效,对我而言就是这种情况。

You're saying that C is the official version of the library and that your minor change (Let's call it C2) would only be used by A. You're also saying that by declaring in A the direct dependency on C2, than it's unfortunately expected that you end up with C2 and C. C will be a transitive dependency from B. Unfortunately too, the Maven reactor treats libraries with the same version but with a different classifier as different libraries, giving you exactly this result (of having both C and C2). 您说的是C是库的正式版本,您的较小更改(我们称它为C2)仅由A使用。您还说的是,通过在A中声明对C2的直接依赖,这很不幸。期望您最终得到C2和C。C将是B的可传递依赖项。不幸的是,Maven反应器也将具有相同版本但具有不同分类器的库视为不同的库,从而为您提供了完全相同的结果(两个C和C2)。

If you only need C2 in project A, you could explicitly add exclusions to C into A's POM. 如果您仅在项目A中需要C2,则可以将排除项显式添加到C到A的POM中。

Obviously this new version of C is for you not a new artifact, but a new version of this artifact. 显然,这个新版本的C对您来说不是一个新的工件,而是这个工件的新版本

So you should not use here a classifier but a version number increase instead. 因此,您不应在此处使用分类器,而应增加版本号。

Your project will be built with the last version (and even if you have a problem to resolve the last version, you can exclude the transitive dependency in your pom) 您的项目将使用最新版本构建(即使您在解决最新版本时遇到问题,也可以在pom中排除传递依赖项)

However, you must be very carefull when doing this, because your B project will have been compiled with the old version of C. So if you modify method erasure, or remove methods or classes, you will have runtime trouble. 但是,执行此操作时必须非常小心,因为您的B项目将使用旧版本的C进行编译。因此,如果修改方法擦除或删除方法或类,则将遇到运行时麻烦。

If you only add classes or methods in your C library, then it should work. 如果仅在C库中添加类或方法,则它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保git存储库中的所有Java项目都具有最新或所需的maven依赖项版本? - How can i ensure all my java projects in a git repository have latest or desired version of maven dependency? 本地 maven 依赖库 - Local maven dependency repository 通过maven下载本地存储库jar依赖文件 - Download local repository jar dependency files through maven Maven - 依赖项未下载到本地存储库 - Maven - dependency not downloading into local repository 如何使gradle覆盖本地Maven存储库中的库? - How can I make gradle overwrite the library in my local maven repo? 休眠版本4 jar,Maven依赖关系不在存储库中-很好,它在存储库中,但是我的存储库错误 - hibernate version 4 jar, maven dependency not in repository - well it is in repository, but my repository is wrong 如何从本地 Maven 存储库中选择依赖项 - How to pick a dependency from local Maven repository Maven在本地存储库上添加依赖项 - maven add dependency located on a local repository Maven无法从我的本地存储库中找到一个jar - Maven can't found a jar from my local repository 如何从项目的maven依赖树中删除旧的易受攻击的Apache commons集合版本依赖项? - How can I remove the old vulnerable Apache commons collection version dependency from my project's maven dependency tree?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM