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

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

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文件中注意到以下内容:

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";version=
"1.1",javax.enterprise.context;uses:="javax.inject";version="1.1&quo
t;,javax.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.event;uses:=
"javax.enterprise.util";version="1.1",javax.enterprise.inject;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 for 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",
javax.enterprise.context;version="1.1",javax.enterprise.context.spi;version
="1.1",javax.enterprise.event;version="1.1",javax.enterprise.inject;ve
rsion="1.1",javax.enterprise.inject.spi;version="1.1",javax.enterprise
.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 包。因此,安装该捆绑包将自动为其他捆绑包提供导入该包的能力。

请注意,如果没有 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 :

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-* 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:

确定