简体   繁体   English

如何从单个 Maven 项目生成多个 OSGi 包?

[英]How can I generate multiple OSGi bundles from a single Maven project?

The basic problem is as such: I've got a project that already uses multiple Maven modules for various sub-projects.基本问题是这样的:我有一个项目,该项目已经将多个 Maven 模块用于各种子项目。 However, one of the modules (the core module) could itself be split into multiple OSGi bundles when created.但是,其中一个模块(核心模块)在创建时本身可以拆分为多个 OSGi 包。 This is due to the core module containing several optional dependencies, each of which have isolated Java packages where they're required.这是因为核心模块包含几个可选的依赖项,每个依赖项在需要的地方都有独立的 Java 包。 For instance, support for JSON input files are optional as they require the optional dependencies from Jackson.例如,对 JSON 输入文件的支持是可选的,因为它们需要 Jackson 的可选依赖项。 The classes that rely on the Jackson dependencies are all isolated to certain json packages within the module.依赖 Jackson 依赖项的类都与模块内的某些json包隔离。 Thus, in theory, I could create a minimal bundle from core that doesn't include the packages that rely on optional dependencies.因此,理论上,我可以从核心创建一个最小包,其中不包括依赖于可选依赖项的包。

Normally, I'd simply split up this module into more Maven modules to make life easier for creating bundles via Felix's maven-bundle-plugin.通常,我会简单地将此模块拆分为更多 Maven 模块,以便通过 Felix 的 maven-bundle-plugin 创建包更容易。 The problem here is that I still want to create a core JAR for non-OSGi users who don't want to have to include several extra JARs just to use optional functionality (which requires they provide the optional dependencies on the class path as it is).这里的问题是我仍然想为非 OSGi 用户创建一个核心 JAR,他们不想为了使用可选功能而包含几个额外的 JAR(这要求他们提供对类路径的可选依赖项,因为它是)。 Not only that, but I don't wish to have to split up this module into more modules as it makes development on the project more tedious for the developers as well, especially when we're already splitting up code into proper package-based modules as it is.不仅如此,我还不想将这个模块拆分成更多的模块,因为这也会让项目的开发对开发人员来说更加乏味,尤其是当我们已经将代码拆分成适当的基于包的模块时照原样。

The way we were trying to use OSGi already was to make the API module a fragment host (in order to allow it to load a provider bundle without requiring OSGi support), then make the other bundles use said fragment host.我们已经尝试使用 OSGi 的方式是让 API 模块成为片段主机(为了允许它加载提供者包而不需要 OSGi 支持),然后让其他包使用所述片段主机。 This seemed to work well for the smaller modules outside of core, but for core, we wanted to be able to provide multiple bundles from a single module so that optional dependencies wouldn't be required in the bundle itself.这对于核心之外的较小模块似乎很有效,但对于核心,我们希望能够从单个模块提供多个捆绑包,以便捆绑包本身不需要可选的依赖项。 As it stands, for plugins, we already have a mechanism for scanning them and ignoring plugins that don't have all the required classes to load them (eg, if a plugin requires a JPA provider but the JPA API is not available, that plugin isn't loaded).就目前而言,对于插件,我们已经有一种机制来扫描它们并忽略没有加载它们所需的所有类的插件(例如,如果插件需要 JPA 提供程序但 JPA API 不可用,则该插件未加载)。 Once we can successfully split up the core module into multiple bundles, I can use declarative services as the plugin method in an OSGi environment (instead of the default class path JAR scanning mechanism in place for normal Java environments), so that isn't an issue.一旦我们可以成功地将核心模块拆分为多个包,我就可以在 OSGi 环境中使用声明式服务作为插件方法(而不是普通 Java 环境中的默认类路径 JAR 扫描机制),所以这不是一个问题。

Is there any way to do all this using Felix's maven-bundle-plugin?有没有办法使用 Felix 的 maven-bundle-plugin 来完成所有这些? Or will I have to use the assembly plugin to copy subsets of the module where bundles can be generated from?或者我是否必须使用程序集插件来复制可以从中生成包的模块的子集? Or will I have to resort to writing an Ant script (or Maven plugin) to do this?或者我必须求助于编写 Ant 脚本(或 Maven 插件)来做到这一点? We've tried using separate Maven modules that simply import the core module as a dependency and generating a bundle from there, but the resultant bundle is always empty regardless of import/export package settings and embed dependencies.我们尝试使用单独的 Maven 模块,这些模块只是将核心模块作为依赖项导入并从那里生成包,但无论导入/导出包设置和嵌入依赖项如何,生成的包始终为空。

Or, is there a better way to do this?或者,有没有更好的方法来做到这一点? We already use the <optional>true</optional> configuration for the optional dependencies, yet the Felix plugin doesn't seem to care about that and imports all of those dependencies anyways without using the optional attribute.我们已经为可选依赖项使用了<optional>true</optional>配置,但 Felix 插件似乎并不关心这一点,并且在不使用 optional 属性的情况下导入所有这些依赖项。

Well, this is what I'm ending up doing to accomplish this.好吧,这就是我最终要完成的工作。 I'm using the maven-assembly-plugin to copy the binaries I need and filtering out the classes I don't want to include using the <fileSets/> element similar to the <fileset/> element in Ant.我正在使用maven-assembly-plugin复制我需要的二进制文件,并使用类似于 Ant 中的<fileset/>元素的<fileSets/>元素过滤掉我不想包含的类。

Using the generated directories for each assembly, I'm using the maven-bundle-plugin along with the <buildDirectory/> configuration option to specify where the bundle's class files are located.使用为每个程序集生成的目录,我使用maven-bundle-plugin<buildDirectory/>配置选项来指定包的类文件所在的位置。

It's not ideal, but it's better than writing an Ant script for a Maven project!不理想,但比为Maven项目编写Ant脚本要好!

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

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