Search code examples
javadependenciesosgiapache-karafosgi-bundle

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


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:

enter image description here

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


Solution

  • 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