简体   繁体   English

在与maven一起使用OSGI包时如何隐藏嵌入式依赖项?

[英]How to hide embedded dependencies when using an OSGI bundle with maven?

Is there a way to use an OSGI bundle as a maven dependency without getting all the packages from it into the classpath that it doesn't even export? 有没有办法使用OSGI包作为maven依赖,而不将其中的所有包都放入甚至不导出的类路径中?

Background of the question: We just added the org.apache.sling.xss bundle as a maven dependency into our project. 问题的背景:我们刚刚将org.apache.sling.xss包作为maven依赖项添加到我们的项目中。 As you can see in the pom.xml , it just exports the package org.apache.sling.xss but embeds various dependencies directly into it's jar as Private-Package . 正如您在pom.xml中看到 ,它只是导出包org.apache.sling.xss,但是将各种依赖项直接嵌入到它的jar中作为Private-Package While OSGI hides this stuff from other bundles, maven does not. 虽然OSGI从其他捆绑包中隐藏了这些东西,但maven却没有。 So we get collisions: org.apache.sling.xss embeds eg version 1.7.0 of commons-beanutils, which is incompatible with the newer version of commons-beanutils we have been using, so that we now get compile errors. 所以我们得到了冲突:org.apache.sling.xss嵌入了例如版本1.7.0的commons-beanutils,它与我们一直使用的较新版本的commons-beanutils不兼容,因此我们现在得到编译错误。

Is there any good solution to this - both from our perspective, as from the perspective of the maintainers of the org.apache.sling.xss bundle? 有没有什么好的解决方案 - 从我们的角度来看,从org.apache.sling.xss包的维护者的角度来看? Ideally, you'd get only the exported org.apache.sling.xss package into the classpath if you use that bundle. 理想情况下,如果使用该包,则只能将导出的org.apache.sling.xss包添加到类路径中。 So maybe a good solution would be if org.apache.sling.xss provided a separate API jar, containing only that class? 所以,如果org.apache.sling.xss提供了一个单独的API jar,那么也许一个很好的解决方案,只包含那个类? Is there a standard way to do this, or another solution? 有没有标准的方法来做到这一点,或另一种解决方案? Can we somehow tell maven just to include only that package into the classpath? 我们能以某种方式告诉maven只将该包包含在类路径中吗? (I know maven has dependency exclusion , but that doesn't help here, since the problematic stuff is not a transitive dependency of sling.xss, but actually included into the org.apache.sling.xss jar.) (我知道maven有依赖项排除 ,但这在这里没有帮助,因为有问题的东西不是sling.xss的传递依赖,但实际上包含在org.apache.sling.xss jar中。)

Unfortunately there is no good way to handle such bundles with maven. 不幸的是,没有好办法用maven处理这样的捆绑。

The recommended way is to split the bundle into an API bundle that just defines the API and an implementation bundle that imports the API and imports or embeds all implementation dependencies. 建议的方法是将捆绑包拆分为仅定义API的API捆绑包以及导入API并导入或嵌入所有实现依赖关系的实现捆绑包。

This way consumers will only have a maven dependency to the API and the problem does not occur at all. 这样,消费者将只对API具有maven依赖性,并且根本不会发生问题。

Can you open an jira issue for sling xss to provide an API bundle? 你可以为吊索xss打开一个jira问题来提供API捆绑吗?

You could include in your build system a step that performs OSGi resolution, for example using the bnd-export-maven-plugin . 您可以在构建系统中包含执行OSGi解析的步骤,例如使用bnd-export-maven-plugin

In this way the build as a whole will fail if you write code that depends on a non-exported package of another bundle. 这样,如果编写依赖于另一个bundle的非导出包的代码,则整个构建将失败。 This is because bnd will automatically add an Import-Package for the package that you used in your bundle, but the resolve step will be unable to find a corresponding Export-Package in the other bundle. 这是因为bnd会自动为您在捆绑包中使用的包添加Import-Package ,但解决步骤将无法在另一个捆绑Export-Package中找到相应的Export-Package

Of course this is not ideal. 当然这并不理想。 You will still be able to write the code in your IDE and compile it with the maven-compiler-plugin . 您仍然可以在IDE中编写代码并使用maven-compiler-plugin编译它。 But you at least find out about the problem before you hit the runtime. 但是你至少在遇到运行时之前就已经发现了问题。

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

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