【问题标题】:How to resolve "Package (...) Cannot be resolved" in Apache Karaf如何在 Apache Karaf 中解决“包 (...) 无法解析”
【发布时间】:2021-01-13 12:32:56
【问题描述】:

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

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

我很确定我必须从 mvnrepository 以 bundles 的形式下载那些红色的“包”,但我只找到 jar 来下载而不是 bundles

【问题讨论】:

    标签: java dependencies osgi apache-karaf osgi-bundle


    【解决方案1】:

    bundle 是一个 jar,其中包含 MANIFEST.MF 中的一些附加数据。

    例如,如果您查看那个罐子: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 可以尝试将您的罐子“包装”成一个包。但是,它可能会导致非最佳捆绑包。当你想要一个特定的包但官方的包对 osgi 不友好时,你可以检查 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

    【讨论】:

    • bundle install -s mvn: 不会工作,因为它是一个离线系统,我可以做的是将 maven-bundle 下载到一个棒上,走到离线系统,安装到本地 .m2 /repo 并调用命令。
    • 请注意,如果有帮助,您可以配置您的 etc/org.ops4j.pax.url.mvn.cfg 以检查指向本地 repo 的 mvn 链接。但是,如果您完全离线工作,您也可以将您的 jar 文件放到部署文件夹中。
    • 是的,完全离线。正如您所建议的,我一直将我需要的所有内容放入部署文件夹中。 :)
    • 嘿,如果我按照您指向 mvnrepsiiory (https://mvnrepository.com/artifact/javax.enterprise/cdi-api/1.2) 的第一个链接,您那里有一个信息表。第 6 行称为 Files,包含 pomjar。与此文件相比,url https://mvnrepository.com/artifact/org.ops4j.pax.cdi/pax-cdi-api/1.1.3 确实包含 bundle。为什么你的 bundle 在 mvnrepository 中没有标记为 bundle
    • 我知道使用 OSGi 环境的人倾向于在他们的 pom.xml 中使用 bundle (只有使用 bundle 插件才能设置),这可能是mvnrepository 如何检查它。不过,这只是一个“好有”,真实信息以MANIFEST.MD为准,我个人只查了
    猜你喜欢
    • 2016-03-28
    • 2020-01-04
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    相关资源
    最近更新 更多