My Question: What is a good way to deal with two different versions of an API? (Sub-question: Is there any way to avoid classpath reference problems if you include two libraries with the same class-names?)
我的问题:处理两个不同版本的API的好方法是什么? (子问题:如果包含两个具有相同类名的库,有没有办法避免类路径引用问题?)
Description of question: I have a project that uses an API. I have spent the last few months developing for the old version and I'm about to add features for a newer version. So far as I can tell there's only one critical difference. However, I don't have the API yet (waiting on someone to get me the jar) so I can't be sure whether there are more differences.
问题描述:我有一个使用API的项目。我花了最近几个月为旧版本开发,我即将为新版本添加功能。据我所知,只有一个关键的区别。但是,我还没有API(等着有人给我拿到罐子)所以我不能确定是否有更多的差异。
Description of subquestion: I'm worried that there may be class reference inconsistencies between the two APIs (like I said, I don't have the jar yet to be sure).
子问题的描述:我担心两个API之间可能存在类引用不一致(就像我说的,我还没有确定jar)。
I realize you may want some code to look at, but this is a design question, not a coding issue. I'm hoping to get some best practices out of this. Thanks!
我意识到你可能想要一些代码来看,但这是一个设计问题,而不是编码问题。我希望得到一些最佳实践。谢谢!
Not a duplicate: I've looked at a few questions which may appear to be duplicates, but they didn't really address my issue :)
不重复:我看了几个可能看似重复的问题,但它们并没有真正解决我的问题:)
2 个解决方案
#1
3
Avoid, or shade.
避免或遮荫。
If you have full control of the classpath you can use ordering: this is brittle and non-intuitive.
如果您完全控制类路径,则可以使用排序:这是脆弱且不直观的。
If you don't, you're at the mercy of whatever is, hence shading, and its related headaches.
如果你不这样做,你就会受到任何影响,因此阴影和相关的头痛。
#2
2
There are also classloader isolation strategies you can use. For example, if you're in a container environment (Java EE, OSGi) you can put the different versions of the libraries in different classloader contexts (i.e., different EJB jars, or different web applications) and they won't interfere with each other.
您还可以使用类加载器隔离策略。例如,如果您处于容器环境(Java EE,OSGi)中,则可以将不同版本的库放在不同的类加载器上下文中(即,不同的EJB jar或不同的Web应用程序),它们不会干扰每个其他。
OSGi can do the same sort of thing; deploy library A.1 and A.2 into your OSGi container, and deploy project.uno (which uses A.1) and project.dos (which uses A.2) into the same JVM, and the OSGi container handles resolution of the classpaths.
OSGi可以做同样的事情;将库A.1和A.2部署到OSGi容器中,并将project.uno(使用A.1)和project.dos(使用A.2)部署到同一个JVM中,OSGi容器处理类路径。
#1
3
Avoid, or shade.
避免或遮荫。
If you have full control of the classpath you can use ordering: this is brittle and non-intuitive.
如果您完全控制类路径,则可以使用排序:这是脆弱且不直观的。
If you don't, you're at the mercy of whatever is, hence shading, and its related headaches.
如果你不这样做,你就会受到任何影响,因此阴影和相关的头痛。
#2
2
There are also classloader isolation strategies you can use. For example, if you're in a container environment (Java EE, OSGi) you can put the different versions of the libraries in different classloader contexts (i.e., different EJB jars, or different web applications) and they won't interfere with each other.
您还可以使用类加载器隔离策略。例如,如果您处于容器环境(Java EE,OSGi)中,则可以将不同版本的库放在不同的类加载器上下文中(即,不同的EJB jar或不同的Web应用程序),它们不会干扰每个其他。
OSGi can do the same sort of thing; deploy library A.1 and A.2 into your OSGi container, and deploy project.uno (which uses A.1) and project.dos (which uses A.2) into the same JVM, and the OSGi container handles resolution of the classpaths.
OSGi可以做同样的事情;将库A.1和A.2部署到OSGi容器中,并将project.uno(使用A.1)和project.dos(使用A.2)部署到同一个JVM中,OSGi容器处理类路径。