Maven-相同依赖项的多个版本 [英] Maven - Multiple version of same dependency

查看:1907
本文介绍了Maven-相同依赖项的多个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,其中的依赖项引入了两个jar:

I have a web application in which the dependencies pull in two jars called:

  1. javassist-3.9.0.GA.jar
  2. javassist-3.20.0-GA.jar

当我打包WAR时,我在WEB-INF/lib目录中都包含了这两个文件,我的问题是该应用程序正在运行,为什么我不会遇到任何问题,因为显然我在两个jars和那里都有相同的类应该是对的吗?

when I package the WAR I have both of these in the WEB-INF/lib directory, my question is that the application is running and why I wouldn't get any issues because apparently I have same classes in both jars and there should be issues right?

推荐答案

对于Java,您提供一个类的多少版本无关紧要.默认的类加载器只会在它可以找到的类路径上选择第一个.

For Java it doesn't matter how many versions of a class you provide. The default classloader will just pick the first one on the classpath it can find.

由于您可以正确运行应用程序,因此这意味着以下情况之一:

Since you can run the application without error this means one of the following:

  • 如果javassist-3.9.0.GA.jar首先出现在类路径中:您的应用程序不依赖新的API或javassist-3.20.0-GA.jar中的错误修正库在这些版本之间进行了更改(库在次要版本之间不应该这样做)

  • if javassist-3.9.0.GA.jar is first on the classpath: your application doesn't rely on new APIs or bugfixes in javassist-3.20.0-GA.jar Also no APIs you used of this library changed between these versions (which a library shouldn't do between minor versions)

如果javassist-3.20.0-GA.jar首先在类路径上:该库是向后兼容的

if javassist-3.20.0-GA.jar is first on the classpath: the library is backwards compatible

我建议:

  • 如果这些依赖关系是应用程序不同部分的直接依赖关系,请确保在所有地方使用相同版本.最好的方法是在父POM的dependencyManagement部分中修复版本,然后在dependencies部分中省略version属性.
  • 如果这些依赖项是传递性依赖项,请排除您不想使用的依赖项,以确保最终应用程序中只有一个版本的库.还可以考虑为仍使用旧版本的项目提出问题,并要求他们升级依赖项的版本.
  • 如果需要使用同一库的两个不兼容版本,它们具有相同的程序包和类名,请考虑使用OSGi之类的模块系统,该模块系统在某种程度上支持运行同一库的不同版本. /li>
  • If these dependencies are direct dependencies in different parts of your application, make sure you're using everywhere the same version. The best way is to fix the version in the dependencyManagement section of the parent POM and then omit the version attribute in the dependencies sections.
  • If these dependencies are transitive dependencies, then exclude the one you don't want to use to make sure you only have one version of the library in your final application. Also consider to file an issue for the project that still uses the old version and ask them to upgrade the version of the dependency.
  • If you need to work with two incompatible versions of the same library, which have the same package and class names, consider to use a module system such as OSGi, which supports running different versions of the same library to some degree.

这篇关于Maven-相同依赖项的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆