在 karaf 中部署第三方 jar 文件时出错

Error deploying third party jar files in karaf

我正在尝试在 apache-karaf 3.0.3 中部署一个包,其中包含我嵌入其中的一定数量的第三方 jar 文件, 因为第三方 jar 文件是非 OSGi 包。其中一个 jar 文件包含 java 文件中不存在的导入语句 不再是最新版本的 jar 文件。(我没有旧版本的 jar 文件)。

e.g: jar file 1 - Class1 - import com.java.test.io

使用 jar 文件部署我的应用程序包时遇到错误。

Error executing command: Error executing command on bundles: Unable to execute command on bundle 391: The bundle "com.test.example.bundle_0.1.0.SNAPSHOT [391]" could not be resolved. Reason: Missing Constraint: Import-Package: com.java.test.io; version="0.0.0"

我正在尝试使用简单的 java 应用程序复制场景,它按预期工作。 我的假设是 karaf 会扫描所有的导入语句并检查是否有合适的导出包(包级权限) 存在于适当的进口声明中。 谁能解释为什么 java 应用程序运行并且在 karaf 中它失败了?

pom.xml

<plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>mybundlename</Bundle-SymbolicName>
                        <Embed-Dependency>jar1,jar2,jar3,jar4</Embed-Dependency>

                    </instructions>
                </configuration>
            </plugin>
        </plugins>

重要的不是 class 中的包导入,而是包的 MANIFEST 中的包导入。

如果您的代码在 OSGi 容器之外工作,那么这意味着在您的特定使用场景中,运行时不需要导入包。或者根本不需要,应该清理掉。

您要么必须部署一个包以满足导入,要么必须在构建包时禁止添加 com.java.test.io 的导入。使用 maven-bundle-plugin 你可以这样实现:

<instructions>
    <Embed-Dependency>...</Embed-Dependency>
    <Import-Package>
        !com.java.test.io.*,
        *
    </Import-Package>
</instructions>