如何解决Apache Karaf中的“包 (…) 无法解析”问题

huangapple go评论95阅读模式
英文:

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

问题

我对Apache Karaf还不熟悉,我是从Felix迁移过来的。

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

如何解决Apache Karaf中的“包 (…) 无法解析”问题

我很确定我必须从mvnrepository下载那些红色的“包”作为bundles,但是除了jar之外,我只找到可以下载的jar

英文:

I am new to Apache Karaf, I migrated from Felix.

I try to run a CDI test but can not resolve the red missing dependencies:

如何解决Apache Karaf中的“包 (…) 无法解析”问题

I am pretty sure I must download those red "packages" as bundles from mvnrepository but instead of bundles I only find jar to download.

答案1

得分: 1

一个捆绑包(bundle)是一个带有附加数据的JAR文件,这些数据存储在MANIFEST.MF文件中。

例如,如果您查看这个JAR文件:https://mvnrepository.com/artifact/javax.enterprise/cdi-api/1.2,您会在META-INF/MANIFEST.MF文件中注意到以下内容:

  1. Manifest-Version: 1.0
  2. Archiver-Version: Plexus Archiver
  3. Created-By: 1.7.0_45 (Oracle Corporation)
  4. Built-By: jharting
  5. Build-Jdk: 1.7.0_45
  6. Implementation-Title: CDI APIs
  7. Implementation-URL: http://cdi-spec.org
  8. Implementation-Vendor: JBoss by Red Hat, Inc.
  9. Implementation-Version: 20140411-1123
  10. Specification-Title: CDI APIs
  11. Specification-Vendor: JBoss by Red Hat, Inc.
  12. Specification-Version: 1.2.0
  13. Export-Package: javax.decorator;uses:="javax.enterprise.inject";version=
  14. "1.1",javax.enterprise.context;uses:="javax.inject";version="1.1&quo
  15. t;,javax.enterprise.inject.spi;uses:="javax.enterprise.context.spi,javax.el,
  16. javax.enterprise.inject,javax.interceptor,javax.enterprise.event";version=
  17. "1.1",javax.enterprise.util;version="1.1",javax.enterprise.event;uses:=
  18. "javax.enterprise.util";version="1.1",javax.enterprise.inject;uses:=
  19. "javax.inject,javax.enterprise.util,javax.enterprise.context";version="1
  20. .1",javax.enterprise.context.spi;version="1.1"
  21. Tool: Bnd-0.0.357
  22. Bundle-Name: CDI APIs
  23. Bundle-Vendor: JBoss by Red Hat, Inc.
  24. Bundle-Version: 1.2.0
  25. Bnd-LastModified: 1397208243348
  26. Bundle-ManifestVersion: 2
  27. Bundle-Description: APIs for CDI (Contexts and Dependency Injection for Java EE)
  28. Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.html
  29. Import-Package: javax.decorator;version="1.1",javax.el;version="2.2",
  30. javax.enterprise.context;version="1.1",javax.enterprise.context.spi;version
  31. ="1.1",javax.enterprise.event;version="1.1",javax.enterprise.inject;ve
  32. rsion="1.1",javax.enterprise.inject.spi;version="1.1",javax.enterprise
  33. .util;version="1.1",javax.inject,javax.interceptor;version="1.2"
  34. Bundle-SymbolicName: javax.enterprise.cdi-api
  35. Bundle-DocURL: http://jboss.org
  36. Name: Build-Information
  37. Maven-Version: 3.1.0
  38. Build-Time: 20140411-1123
  39. Os-Name: Linux
  40. Java-Version: 1.7.0_45
  41. Java-Vendor: Oracle Corporation
  42. Os-Version: 3.7.3-101.fc17.x86_64
  43. Os-Arch: amd64
  44. SCM: 5a0981caa28053c49f9e1932ba1f629a51e355c2

其中,以 "Bundle-" 开头的部分对您有所帮助。例如,Export-Package 包含了 javax.enterprise.context;uses:=...;version="1.1",这表明该捆绑包将导出 javax.enterprise.context 包。因此,安装该捆绑包将自动为其他捆绑包提供导入该包的能力。

请注意,如果没有 Export-Package,我知道 Karaf 可以尝试将您的JAR文件“包装”成一个捆绑包。然而,这可能会导致不太优化的捆绑包。当您需要一个特定的包但官方版本不符合OSGi标准时,您可以查看 servicemix 组织,如果有专门创建的组织的话:https://mvnrepository.com/artifact/org.apache.servicemix。

对于您的特定情况,运行 bundle install -s mvn:javax.enterprise/cdi-api/1.2(或将JAR文件放入 deploy 文件夹)应该可以解决问题。

我强烈鼓励您阅读有关OSGi的三个层次(模块化、生命周期和服务),以更好地理解Karaf在幕后执行的操作。例如,可以阅读这本书,当然您也可以在互联网上找到很多有关信息:https://www.manning.com/books/osgi-in-action

英文:

A bundle is a jar with some additional data in the MANIFEST.MF.

For example, if you look at that jar : https://mvnrepository.com/artifact/javax.enterprise/cdi-api/1.2, you'll notice in META-INF/MANIFEST.MF those lines :

  1. Manifest-Version: 1.0
  2. Archiver-Version: Plexus Archiver
  3. Created-By: 1.7.0_45 (Oracle Corporation)
  4. Built-By: jharting
  5. Build-Jdk: 1.7.0_45
  6. Implementation-Title: CDI APIs
  7. Implementation-URL: http://cdi-spec.org
  8. Implementation-Vendor: JBoss by Red Hat, Inc.
  9. Implementation-Version: 20140411-1123
  10. Specification-Title: CDI APIs
  11. Specification-Vendor: JBoss by Red Hat, Inc.
  12. Specification-Version: 1.2.0
  13. Export-Package: javax.decorator;uses:="javax.enterprise.inject";versio
  14. n="1.1",javax.enterprise.context;uses:="javax.inject";version="1.1",j
  15. avax.enterprise.inject.spi;uses:="javax.enterprise.context.spi,javax.
  16. el,javax.enterprise.inject,javax.interceptor,javax.enterprise.event";
  17. version="1.1",javax.enterprise.util;version="1.1",javax.enterprise.ev
  18. ent;uses:="javax.enterprise.util";version="1.1",javax.enterprise.inje
  19. ct;uses:="javax.inject,javax.enterprise.util,javax.enterprise.context
  20. ";version="1.1",javax.enterprise.context.spi;version="1.1"
  21. Tool: Bnd-0.0.357
  22. Bundle-Name: CDI APIs
  23. Bundle-Vendor: JBoss by Red Hat, Inc.
  24. Bundle-Version: 1.2.0
  25. Bnd-LastModified: 1397208243348
  26. Bundle-ManifestVersion: 2
  27. Bundle-Description: APIs for CDI (Contexts and Dependency Injection fo
  28. r Java EE)
  29. Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.html
  30. Import-Package: javax.decorator;version="1.1",javax.el;version="2.2",j
  31. avax.enterprise.context;version="1.1",javax.enterprise.context.spi;ve
  32. rsion="1.1",javax.enterprise.event;version="1.1",javax.enterprise.inj
  33. ect;version="1.1",javax.enterprise.inject.spi;version="1.1",javax.ent
  34. erprise.util;version="1.1",javax.inject,javax.interceptor;version="1.2"
  35. Bundle-SymbolicName: javax.enterprise.cdi-api
  36. Bundle-DocURL: http://jboss.org
  37. Name: Build-Information
  38. Maven-Version: 3.1.0
  39. Build-Time: 20140411-1123
  40. Os-Name: Linux
  41. Java-Version: 1.7.0_45
  42. Java-Vendor: Oracle Corporation
  43. Os-Version: 3.7.3-101.fc17.x86_64
  44. Os-Arch: amd64
  45. SCM: 5a0981caa28053c49f9e1932ba1f629a51e355c2

Bundle-* are the ones that interest you. For example, Export-Package contains javax.enterprise.context;uses:=...;version="1.1" which indicates that this bundle will export the package javax.enterprise.context.

So installing that bundle, will automatically provide to your other bundles the ability to Import-Package that package.

Note that if there wasn't any Export-Package, I know karaf can try to "wrap" your jar into a bundle. However, it can leads to non really optimal bundles. When you want a particular package but the official one is not osgi-friendly, you can check the servicemix organization, if any has been created specifically : https://mvnrepository.com/artifact/org.apache.servicemix.

In your specific case, a bundle install -s mvn:javax.enterprise/cdi-api/1.2 (or dropping the jar in the deploy folder) should do the trick.

I highly encourage you to read the three OSGI layers (modularity, lifecycle & services) to better understand what karaf is doing behind the scene. For example this book, but you can surely find good informations on the internet too : https://www.manning.com/books/osgi-in-action

huangapple
  • 本文由 发表于 2020年9月28日 05:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定