gpt4 book ai didi

java - 在OSGI中导入内部包

转载 作者:行者123 更新时间:2023-12-02 03:32:40 25 4
gpt4 key购买 nike

我最近开始将现有的 Maven 项目转换为 OSGI 包,并且对我需要在 <Import-Packages> 中列出的内容感到困惑POM 文件中的标记。

最初,我用星号来查看 OSGI 认为需要的所有内容,并惊讶地发现它列出了来自同一项目的包。我在网上查了一下,但无法找到是否需要导入内部包以及不这样做是否会出现潜在问题的明确答案。

我是否需要列出<Import-Packages>中的项目中使用过的包,如果是的话为什么,因为对我来说似乎没有必要。

我在 POM 中使用以下插件:
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>

这是 tx-core 生成的 pom。所有 tx-core.* 包都来自项目本身。

Manifest-Version: 1.0
Bundle-SymbolicName: tx-core
Archiver-Version: Plexus Archiver
Built-By: User
Bnd-LastModified: 1466089543997
Bundle-ManifestVersion: 2
Import-Package: cafe.crypto,tx.core,tx.core.conf,tx.core.conv,tx.core.
production,tx.core.query,tx.core.util,javax.crypto,javax.crypto.spec,
javax.servlet
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
DynamicImport-Package: javax.*, org.xml.sax, org.xml.sax.*,org.w3c.*
Tool: Bnd-3.0.0.201509101326
Export-Package: cafe.crypto;version="4.0.1",tx.core;uses:="tx.core.pro
duction,tx.core.query";version="4.0.1",tx.core.conf;uses:="tx.core,tx
.core.production";version="4.0.1",tx.core.conv;version="4.0.1",tx.cor
e.identity;version="4.0.1",tx.core.io;uses:="tx.core,tx.core.producti
on";version="4.0.1",tx.core.notify;version="4.0.1",tx.core.production
;uses:="tx.core,tx.core.conv,tx.core.query";version="4.0.1",tx.core.q
uery;uses:="tx.core.conv";version="4.0.1",tx.core.util;version="4.0.1
",tx;version="4.0.1"
Bundle-Name: tx-core
Bundle-Version: 4.0.1
Created-By: Apache Maven Bundle Plugin
Build-Jdk: 1.8.0_77

感谢您提供的所有帮助。

最佳答案

对于给定 bundle 内部的包,无需导入它们。 <Import-Packages>用于指示跨 bundle 所需的包。

如果没有看到您的 POM/MANIFEST 或不知道您用于 OSGi 捆绑的 Maven 插件,我不完全确定为什么星号会显示所有内部包。我怀疑你的 POM/MANIFEST 正在使用 <Export-Package>导出所有内部包。

更新来自 Apache Felix Maven Bundle Plugin Docs :

<Import-Package> is assumed to be "*", which imports everything referred to by the bundle content, but not contained in the bundle. Any exported packages are also imported by default, to ensure a consistent class space.

参见Should a bundle import its own exported packages?了解为什么会发生这种情况。

默认情况下,如果 <Export-Package>没有明确定义,那么所有的包(有异常(exception))都会被导出然后重新导入:

<Export-Package> is now assumed to be the set of packages in your local Java sources, excluding the default package '.' and any packages containing 'impl' or 'internal'.

对于 bundle 内部的包,如果您不希望其他 bundle 能够引用它们,您有 2 个选择:

  1. 定义您想要<Export-Package>中的其他 bundle 公开的软件包的明确列表。
  2. 定义您不希望<Private-Package> 中的其他 bundle 公开的软件包的显式列表。

关于java - 在OSGI中导入内部包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37863683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com