如何在 Apache Karaf 中解析 "Package (...) Cannot be resolved"

How to resolve "Package (...) Cannot be resolved" in Apache Karaf

我是 Apache Karaf 的新手,我是从 Felix 迁移过来的。

我尝试运行 CDI 测试但无法解决红色缺少的依赖项:

我很确定我必须从 mvnrepository 下载那些红色的“包”作为 bundles 但不是 bundles 我只找到 jar 来下载。

捆绑包是在 ​​MANIFEST.MF.

中包含一些额外数据的 jar

例如,如果您查看那个罐子:https://mvnrepository.com/artifact/javax.enterprise/cdi-api/1.2,您会在 META-INF/MANIFEST.MF 中注意到这些行:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: 1.7.0_45 (Oracle Corporation)
Built-By: jharting
Build-Jdk: 1.7.0_45
Implementation-Title: CDI APIs
Implementation-URL: http://cdi-spec.org
Implementation-Vendor: JBoss by Red Hat, Inc.
Implementation-Version: 20140411-1123
Specification-Title: CDI APIs
Specification-Vendor: JBoss by Red Hat, Inc.
Specification-Version: 1.2.0
Export-Package: javax.decorator;uses:="javax.enterprise.inject";versio
 n="1.1",javax.enterprise.context;uses:="javax.inject";version="1.1",j
 avax.enterprise.inject.spi;uses:="javax.enterprise.context.spi,javax.
 el,javax.enterprise.inject,javax.interceptor,javax.enterprise.event";
 version="1.1",javax.enterprise.util;version="1.1",javax.enterprise.ev
 ent;uses:="javax.enterprise.util";version="1.1",javax.enterprise.inje
 ct;uses:="javax.inject,javax.enterprise.util,javax.enterprise.context
 ";version="1.1",javax.enterprise.context.spi;version="1.1"
Tool: Bnd-0.0.357
Bundle-Name: CDI APIs
Bundle-Vendor: JBoss by Red Hat, Inc.
Bundle-Version: 1.2.0
Bnd-LastModified: 1397208243348
Bundle-ManifestVersion: 2
Bundle-Description: APIs for CDI (Contexts and Dependency Injection fo
 r Java EE)
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.html
Import-Package: javax.decorator;version="1.1",javax.el;version="2.2",j
 avax.enterprise.context;version="1.1",javax.enterprise.context.spi;ve
 rsion="1.1",javax.enterprise.event;version="1.1",javax.enterprise.inj
 ect;version="1.1",javax.enterprise.inject.spi;version="1.1",javax.ent
 erprise.util;version="1.1",javax.inject,javax.interceptor;version="1.2"
Bundle-SymbolicName: javax.enterprise.cdi-api
Bundle-DocURL: http://jboss.org

Name: Build-Information
Maven-Version: 3.1.0
Build-Time: 20140411-1123
Os-Name: Linux
Java-Version: 1.7.0_45
Java-Vendor: Oracle Corporation
Os-Version: 3.7.3-101.fc17.x86_64
Os-Arch: amd64
SCM: 5a0981caa28053c49f9e1932ba1f629a51e355c2

Bundle-* 是您感兴趣的。例如,Export-Package 包含 javax.enterprise.context;uses:=...;version="1.1",表示此捆绑包将导出包 javax.enterprise.context.

所以安装那个包,会自动为你的其他包提供Import-Package那个包的能力。

请注意,如果没有 Export-Package,我知道 karaf 可以尝试将您的 jar 包“打包”成一个包。但是,它可能导致非真正最优的捆绑。当你想要一个特定的包但官方包不是 osgi-friendly 时,你可以检查 servicemix 组织,如果有专门创建的:https://mvnrepository.com/artifact/org.apache.servicemix.

在您的特定情况下,bundle install -s mvn:javax.enterprise/cdi-api/1.2(或将 jar 放到部署文件夹中)应该可以解决问题。

我强烈建议您阅读 OSGI 的三个层(模块化、生命周期和服务),以更好地了解 karaf 在幕后做了什么。例如这本书,但你肯定也可以在互联网上找到好的信息:https://www.manning.com/books/osgi-in-action