[英]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.