简体   繁体   English

如何在pom.xml中使用两个版本的itext jar文件?

[英]How use two verions of itext jar files in pom.xml?

In my project i have dependencies displaytag and birt-runtime . 在我的项目中,我有依赖项displaytagbirt-runtime And here display tag needs itext-2.0.1.jar and birt-runtime needs itext-2.1.7.jar so how can i declare this in my pom.xml to use itext-2.0.1.jar only by displaytag and use itext-2.1.7.jar only by birt-runtime . 这里显示标签需要itext-2.0.1.jarbirt-runtime需要itext-2.1.7.jar所以我如何在pom.xml声明它仅通过displaytag使用itext-2.0.1.jar并使用itext-2.1.7.jar仅由birt-runtime could some one let me know how to achieve this? 有人可以让我知道如何实现吗? Thanks in advance. 提前致谢。

In normal java application this is not possible, because in the application itext 2.1 and 2.0 will share the same classloader. 在普通的Java应用程序中,这是不可能的,因为在应用程序中,extx 2.1和2.0将共享相同的类加载器。 But normally, java-apis take care about backward-compatibility, so it should be possible to include only 2.1. 但是通常,java-apis会注意向后兼容性,因此应该只包含2.1。

If not, you need multiple classloaders and then it will become complicated. 如果不是,则需要多个类加载器,这将变得很复杂。

Existing solutions: 现有解决方案:

  • You could try to add an OSGi container to your application and run both dependencies as a separate osgi-bundle. 您可以尝试将OSGi容器添加到应用程序中,并将这两个依赖项作为单独的osgi-bundle运行。
  • If you run a jboss application server, you could create one module with displaytag and another one with birt-runtime. 如果运行jboss应用程序服务器,则可以使用displaytag创建一个模块,并使用birt-runtime创建另一个模块。

DIY: DIY:

  • I've never done this but you could try to create to manage your own classloaders in your application and load the dependent jars into each own classloader. 我从来没有做过,但是您可以尝试在应用程序中创建管理自己的类加载器,并将相关的jar加载到每个自己的类加载器中。 This article seems to cover the topic. 本文似乎涵盖了该主题。

Short answer: you can't. 简短的答案:您不能。

Long answer: Maven is a build tool and has no effect on runtime class loading in your application. 长答案:Maven是一个构建工具,对应用程序中运行时类的加载没有影响。 Normally what it generates is one (or more) artifact(s), tipically jar or war files that may or may not contain your project's dependencies (depending on your POM files). 通常,它生成的是一个(或多个)工件,通常是jar或war文件,它们可能包含也可能不包含项目的依赖项(取决于POM文件)。

What you want to achieve is done at runtime by class loaders but under normal circumstances you don't want to tamper with class loading. 您想要实现的目标是由类加载器在运行时完成的,但是在正常情况下,您不想篡改类加载。

What you could do is: 您可以做的是:

  1. Exclude unnecessary transitive dependencies of a dependency in your pom by defining exclusions , this way only one version of itext would be used. 通过定义排除项来排除 pom中依赖项的不必要的传递性依赖项,这种方式只能使用itext的一个版本。 Of course, this only works if your dependencies don't rely on the internals of itext and their public API's are compatible but this might be the cleanest and easiest solution. 当然,这仅在您的依赖项不依赖于itext内部并且它们的公共API兼容的情况下才有效,但这可能是最干净,最简单的解决方案。

  2. Use a framework/container that has stronger control over class loading, such as an OSGi container. 使用对类加载具有更强控制能力的框架/容器,例如OSGi容器。 These container provide bundles (somewhat equivalent to artifacts) with "private" class loaders enabling your application having multiple versions of the same library loaded that don't interfere with each other. 这些容器提供了带有“私有”类加载器的捆绑软件(在某种程度上等同于工件),使您的应用程序可以加载相同库的多个版本,而不会互相干扰。 This solution, however, has other disadvantages and I would only recommend this if you're already familiar with OSGi. 但是,该解决方案还有其他缺点,只有在您已经熟悉OSGi的情况下,我才建议这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM