Java 我是否可以强制maven依赖项a使用依赖项B的特定版本,同时使用不同版本的依赖项B?
我有两个maven项目,让我们称它们为Java 我是否可以强制maven依赖项a使用依赖项B的特定版本,同时使用不同版本的依赖项B?,java,maven,dependencies,pom.xml,diamond-problem,Java,Maven,Dependencies,Pom.xml,Diamond Problem,我有两个maven项目,让我们称它们为master和auxMaster依赖于aux,也依赖于特定版本的org.apache.httpcomponents.httpclientAux依赖于更高版本的org.apache.httpcomponents.httpclient e、 g 主人 我的公司 org.apache.httpcomponents httpclient 早期版本 我的公司 辅助的 ... ... 但是,aux依赖于仅在较新版本的httpclient中找到的类,并且master对
master
和aux
Master
依赖于aux
,也依赖于特定版本的org.apache.httpcomponents.httpclient
Aux
依赖于更高版本的org.apache.httpcomponents.httpclient
e、 g
主人
我的公司
org.apache.httpcomponents
httpclient
早期版本
我的公司
辅助的
...
...
但是,aux
依赖于仅在较新版本的httpclient
中找到的类,并且master
对httpclient
的依赖关系不向前兼容,因此无论我排除哪个版本,REST调用都会在预期位置失败
是否有办法要求aux
使用较新的依赖项,以及master
使用较旧的依赖项
我知道我可以通过修补
aux
和master
来协调它们,使它们能够使用相同的依赖项,但这远远不够理想。您可以编写自定义类加载器来加载指定版本的类,因为默认类加载器只会选择它能找到的类路径上的第一个版本。不能同时在类路径上有同一库/类的多个版本。您必须将“master”和“aux”分离到单独的jar中,嵌入所需版本的httpclient
,并用自己的类加载器加载每个jar
有一个称为OSGi的框架正是这样做的。对于您的应用程序来说,这可能有些过分,但是如果您想开始,您可以看一看。简短回答:不。请看,您必须制作一个新版本的aux,它使用较新版本的http,否则您会丢失……OSGi肯定比管理您自己的类加载器图更简单!
<project...>
<artifactId>master</artifactId>
<groupId>com.my-company</groupId>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>earlier version</version>
</dependency>
<!--<uses later version of http client>-->
<dependency>
<groupId>com.my-company</groupId>
<artifactId>aux</artifactId>
</dependency>
...
</dependencies>
...
</project>