3

We want to replace certain resources in a host OSGi bundle by adding an OSGi fragment.

As I understand it, the resources included in an OSGi fragment are merely added to the classpath of the host bundle. There is no guarantee that if the fragment is adding a resource that already exists in the host bundle, the resource from the fragment will be loaded: it could also still be the host version.

In order to make sure the fragment version of the resource is loaded instead of the host version, http://wiki.osgi.org/wiki/Fragment mentions it is possible to use the Bundle-ClassPath header to specify resources as "first"(preferred).

It gives patch.jar as an example:

Bundle-ClassPath: patch.jar,.

As mentioned there: "Since patch.jar is ahead of '.' it will allow classes to be preferentially loaded from the fragment instead of the host."

I could use this technique, but this means I first have to bundle my fragment resources in a separate jar and then include this jar in the fragment bundle.

Is there a way to preferentially load classes/resources from the fragment instead of the host without having to include a jar in the fragment?

4

3 回答 3

4

你不必包括一个罐子。您可以改为使用“补丁”目录,然后将您的类放在片段中的补丁目录中。

于 2014-01-20T18:08:12.660 回答
1

对于那些仍在苦苦挣扎的人,这些是对我有用的确切步骤:

  1. 使用要替换的资源/类创建片段项目

  2. 在片段的 build.properties 中,更改source.. = src/output.. = bin/tosource.patch/ = src/output.patch/ = bin/

  3. 在片段的清单中,添加patch/到捆绑包类路径

示例

假设您有com.example.ui一个插件,其中有一个com.example.ui.MessageDialog要替换的类。

  • 创建片段项目com.example.ui.fragment

  • 在包中创建MessageDialogcom.example.ui(不是com.example.ui.fragment);

  • 像这样编辑片段的 build.properties 文件:

    source.patch/ = src/
    output.patch/ = bin/
    
  • 在片段的清单中添加:

    Bundle-ClassPath: patch/
    
  • com.example.ui清单中更改捆绑类路径:

    Bundle-ClassPath: patch/,. 
    
于 2014-12-08T09:25:38.560 回答
1

好的,有几种方法可以完成您想要的。据我了解,完成所有操作后,您希望从库包中导出包,但补丁包中的包除外。

为此,在库包的 Manifest.MF 中,指定要导出的显式包而不是“.”。您可以使用片段来执行此操作,这样您就不必修改原始包。然后,对您的补丁包执行相同的操作。

另一种选择是使用 maven-bundle-plugin 将补丁包和库包“遮蔽”(合并)到一个新包中。只要补丁包和库包的版本号不同,这也可以。许多博客会指导您使用 maven-shade-plugin 和 maven-bundle-plugin 来实现此选项,但它们是不正确的。您绝对可以使用 maven-bundle-plugin 执行着色。

于 2014-01-20T17:46:30.803 回答