OSGi 环境在运行时未找到 Maven jar 依赖项

发布于 2025-01-13 05:07:12 字数 4738 浏览 0 评论 0原文

我正在构建一个需要一些外部依赖项的 Eclipse 产品,这些依赖项没有作为 Eclipse 插件捆绑在一起。 例如javax.json-1.1.4.jar

我正在使用一个目标平台文件,并添加了 Maven 依赖项。这是 .target 文件的相关部分:

<location includeDependencyScope="compile" includeSource="true" missingManifest="generate" type="Maven">
            <dependencies>
                ......
                <dependency>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.json</artifactId>
                    <version>1.1.4</version>
                    <type>jar</type>
                </dependency>
            </dependencies>
        </location>

使用此 Json 实现的插件包含生成的包:这是插件的 MANIFEST

Require-Bundle: org.glassfish.javax.json;bundle-version="1.1.4"

插件正常编译和运行。问题发生在运行时,当加载 Json 实现时:

2022-03-11 09:44:18,166 ERROR [main]: Provider org.glassfish.json.JsonProviderImpl not found
2022-03-11 09:44:18,168 ERROR [main]: 
javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
    at javax.json.spi.JsonProvider.provider(JsonProvider.java:99)
    at javax.json.Json.createReader(Json.java:225)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.MotorDataHandler.parseMotorJsonFile(MotorDataHandler.java:64)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.initMotorsDB(DBHandler.java:209)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.getMotors(DBHandler.java:116)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.getMotors(FieldBusDevice.java:1323)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.createFromSiriusString(FieldBusDevice.java:1257)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.HwConfiguratorFactoryImpl.createFieldBusDeviceFromString(HwConfiguratorFactoryImpl.java:252)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.HwConfiguratorFactoryImpl.createFromString(HwConfiguratorFactoryImpl.java:93)
    at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.createFromString(XMLHelperImpl.java:1615)
    at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.setValue(XMLHelperImpl.java:1156)
    at org.eclipse.emf.ecore.xmi.impl.XMLHandler.setFeatureValue(XMLHandler.java:2710)
    at org.eclipse.emf.ecore.xmi.impl.XMLHandler.setAttribValue(XMLHandler.java:2769)
    at org.eclipse.emf.ecore.xmi.impl.SAXXMIHandler.handleObjectAttribs(SAXXMIHandler.java:79)
    at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectFromFactory(XMLHandler.java:2247)
Caused by: java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl cannot be found by javax.json-api_1.1.4
    at org.eclipse.osgi.internal.loader.BundleLoader.generateException(BundleLoader.java:516)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:511)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:403)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at javax.json.spi.JsonProvider.provider(JsonProvider.java:96)
    at javax.json.Json.createReader(Json.java:225)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.MotorDataHandler.parseMotorJsonFile(MotorDataHandler.java:64)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.initMotorsDB(DBHandler.java:209)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.getMotors(DBHandler.java:116)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.getMotors(FieldBusDevice.java:1323)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.createFromSiriusString(FieldBusDevice.java:1257)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.HwConfiguratorFactoryImpl.createFieldBusDeviceFromString(HwConfiguratorFactoryImpl.java:252)

api jar javax.json-api_1.1.4< 在运行时找不到 javax.json-1.1.4.jar /代码>。

我发现使它工作的唯一方法是将实现 jar 添加到插件的运行时类路径设置中,在 Bundle-Classpath 中:

Bundle-ClassPath: .,
 lib/javax.json-1.1.4.jar,

这需要插件的 lib 文件夹中的 jar,而它已经包含在目标中平台。应该够了.. 是否需要进行配置或执行某些操作才能使 OSGi 环境在运行时将 jar 识别为 Maven 依赖项? 我已经阅读过有关 Eclipse-BuddyPolicy 和 DynamicImport-Package 的内容,但我不知道在我的情况下如何使用它们,以及它们是否有用。

这个 https://github.com/eclipse-ee4j/jsonp/issues/96 说它“应该用 javax.json:1.1.4 和 jakarta.json:1.1.5 修复”,但我不这样做 得到如何...

I'm building an Eclipse product that requires some external dependencies, which are not bundled as Eclipse plugins.
For example javax.json-1.1.4.jar.

I'm usign a target platform file, with Maven dependency added. This is the relevant part of the .target file:

<location includeDependencyScope="compile" includeSource="true" missingManifest="generate" type="Maven">
            <dependencies>
                ......
                <dependency>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.json</artifactId>
                    <version>1.1.4</version>
                    <type>jar</type>
                </dependency>
            </dependencies>
        </location>

The resulting bundle is included from the plugin that uses this Json implementation: this is the MANIFEST of the plugin

Require-Bundle: org.glassfish.javax.json;bundle-version="1.1.4"

The plugin compiles and run normally. The problem happens at runtime, when the Json implementation is loaded:

2022-03-11 09:44:18,166 ERROR [main]: Provider org.glassfish.json.JsonProviderImpl not found
2022-03-11 09:44:18,168 ERROR [main]: 
javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
    at javax.json.spi.JsonProvider.provider(JsonProvider.java:99)
    at javax.json.Json.createReader(Json.java:225)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.MotorDataHandler.parseMotorJsonFile(MotorDataHandler.java:64)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.initMotorsDB(DBHandler.java:209)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.getMotors(DBHandler.java:116)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.getMotors(FieldBusDevice.java:1323)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.createFromSiriusString(FieldBusDevice.java:1257)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.HwConfiguratorFactoryImpl.createFieldBusDeviceFromString(HwConfiguratorFactoryImpl.java:252)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.HwConfiguratorFactoryImpl.createFromString(HwConfiguratorFactoryImpl.java:93)
    at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.createFromString(XMLHelperImpl.java:1615)
    at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.setValue(XMLHelperImpl.java:1156)
    at org.eclipse.emf.ecore.xmi.impl.XMLHandler.setFeatureValue(XMLHandler.java:2710)
    at org.eclipse.emf.ecore.xmi.impl.XMLHandler.setAttribValue(XMLHandler.java:2769)
    at org.eclipse.emf.ecore.xmi.impl.SAXXMIHandler.handleObjectAttribs(SAXXMIHandler.java:79)
    at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectFromFactory(XMLHandler.java:2247)
Caused by: java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl cannot be found by javax.json-api_1.1.4
    at org.eclipse.osgi.internal.loader.BundleLoader.generateException(BundleLoader.java:516)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:511)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:403)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at javax.json.spi.JsonProvider.provider(JsonProvider.java:96)
    at javax.json.Json.createReader(Json.java:225)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.MotorDataHandler.parseMotorJsonFile(MotorDataHandler.java:64)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.initMotorsDB(DBHandler.java:209)
    at com.test.mas.rcp.hwconfigurator.sirius.core.utils.DBHandler.getMotors(DBHandler.java:116)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.getMotors(FieldBusDevice.java:1323)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.FieldBusDevice.createFromSiriusString(FieldBusDevice.java:1257)
    at com.test.mas.rcp.hwconfigurator.sirius.core.impl.HwConfiguratorFactoryImpl.createFieldBusDeviceFromString(HwConfiguratorFactoryImpl.java:252)

The javax.json-1.1.4.jar is not found at runtime by the api jar javax.json-api_1.1.4.

The only way I found to make it work is to add the implementation jar to the runtime classpath settings of the plugin, in the Bundle-Classpath:

Bundle-ClassPath: .,
 lib/javax.json-1.1.4.jar,

This requires the jar in the lib folder of the plugin, while it is already included from the tartget platform. It should be enough..
Is there a configuration or something to be done to make the OSGi environment recognise the jar as a Maven dependency at runtime?
I have read about Eclipse-BuddyPolicy and DynamicImport-Package but I don't know how to used them in my case, and if they are usefull.

This https://github.com/eclipse-ee4j/jsonp/issues/96 says that it "Should be fixed with javax.json:1.1.4 and jakarta.json:1.1.5" but I don't
get how...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2025-01-20 05:07:12

感谢此线程中的评论,我设法解决了这个问题:

https ://github.com/eclipse-ee4j/jax-ws-api/issues/90

特别是最后一个:

https://github.com/eclipse-ee4j/jax- ws-api/issues/90#issuecomment-952793454

这与我在实施时遇到的一个类似的问题有关
com.sun.xml.ws.spi.ProviderImpl Web 服务提供商。

包括 OSGi 资源定位器捆绑清单和具有实际实现的插件使它们在运行时可被发现。

这是目标平台文件中的依赖关系:

<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>osgi-resource-locator</artifactId>
    <version>2.5.0-b42</version>
    <type>jar</type>
</dependency>

最后,针对 Json 问题,我切换到了 Eclipse Parsson 实现,它在运行时工作没有问题。
这些是目标平台中所需的依赖项:

<dependency>
    <groupId>org.eclipse.parsson</groupId>
    <artifactId>jakarta.json</artifactId>
    <version>1.0.0</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.eclipse.parsson</groupId>
    <artifactId>parsson</artifactId>
    <version>1.0.0</version>
    <type>jar</type>
</dependency>

I managed to fix this problem, thanks to the comments in this thread:

https://github.com/eclipse-ee4j/jax-ws-api/issues/90

in particular the last one:

https://github.com/eclipse-ee4j/jax-ws-api/issues/90#issuecomment-952793454

This is related to a veri similar problem I had with the implementation of the
com.sun.xml.ws.spi.ProviderImpl web service provider.

Including the OSGi Resource Locator in the bundle manifest, and the plugin with the actual implementations, make them discoverable at runtime.

This is the dependency in the target platform file:

<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>osgi-resource-locator</artifactId>
    <version>2.5.0-b42</version>
    <type>jar</type>
</dependency>

In the end, for the Json problem, I switched to the Eclipse Parsson implementation which works at runtime without problems.
These are the needed dependencies in the target platform:

<dependency>
    <groupId>org.eclipse.parsson</groupId>
    <artifactId>jakarta.json</artifactId>
    <version>1.0.0</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.eclipse.parsson</groupId>
    <artifactId>parsson</artifactId>
    <version>1.0.0</version>
    <type>jar</type>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文