Jar build as a OSGI bundle not resolving dependencies


(Gaurav Sharma) #1

Hi, I’m new to Gradle. I’m trying to create a OSGI bundle with rest services in it. But, before my rest services work, I’m not able to run the bundle in Karaf container. At the same time the cxf example in apache servicemix package works fine. I keep getting the following error -

org.osgi.framework.BundleException: Unresolved constraint in bundle CACHE [44]: Unable to resolve 44.0: missing requirement [44.0] package; (&(package=javax.ws.rs)(version>=2.0.0)(!(version>=3.0.

When I copy the javax jar in the deploy folder this error is removed and another missing requirement error pops up. From what I can understand is that at runtime, it is not able to get the dependecies. Though, I have also copied my dependencies in a lib folder inside the packaged jar. I’m putting my build script here, I checked over the internet but could not find anything that could make it work. Please advice. I’m short of time and have to complete this for a demo.

build script ----

apply plugin: 'java'
 apply plugin: 'osgi'
 apply plugin: 'groovy'
 apply plugin: 'application'
        repositories {
       mavenCentral()
    }
   configurations {
     extraLibs
 }
   dependencies {
    compile group: 'net.sf.ehcache', name: 'ehcache', version: '2.8.0'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0'
    compile group: 'org.osgi', name: 'osgi_R4_core', version: '1.0'
    compile 'org.json:json:20131018'
         testCompile 'junit:junit:4.8.2'
         extraLibs 'net.sf.ehcache:ehcache:2.8.0'
    extraLibs 'org.slf4j:slf4j-api:1.7.5'
    extraLibs 'javax.ws.rs:javax.ws.rs-api:2.0'
    extraLibs 'org.json:json:20131018'
         }
  // startScripts.classpath += files('$APP_HOME/lib/*')
      version = '1.0'
      jar {
                  into('') {
         from configurations.extraLibs
              }
               manifest {
         version = '1.0.0'
         symbolicName = 'CACHE'
         instruction 'Bundle-Activator', 'com.cache.activator.CacheActivator'
       }
    }

thank you!


(Gaurav Sharma) #2

I’m building the jar through eclipse.


(Luke Daley) #3

I have also copied my dependencies in a lib folder inside the packaged jar

Based on your posted script, you aren’t doing this. You would need:

jar {
        into('lib') {
            from configurations.extraLibs
                    }
}

(gaurav sharma) #4

Hi, im sorry! I have been constantly making changes hence missed it but when I have lib updated as you mentioned then also I face the same error as I earlier mentiobed in my code…


(Luke Daley) #5

I think you need extra instructions in the osgi manifest when embedding jars. I don’t think it implicitly looks for them.


(Gaurav Sharma) #6

Hi Luke, any idea which instructions should i add?


(Luke Daley) #7

I think it’s ‘Bundle-Classpath’.

http://wiki.osgi.org/wiki/Bundle-ClassPath


(Gaurav Sharma) #8

Hi, I made changes in my gradle build but still servicemix is not able to resolve the dependencies which are packaged in the bundle itself -

apply plugin: 'java'
 apply plugin: 'osgi'
// apply plugin: 'groovy'
 apply plugin: 'application'
         repositories {
       mavenCentral()
    }
   configurations {
     extraLibs
 }
   dependencies {
    compile group: 'net.sf.ehcache', name: 'ehcache', version: '2.8.0'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0'
    compile group: 'org.osgi', name: 'osgi_R4_core', version: '1.0'
    compile 'org.json:json:20131018'
         testCompile 'junit:junit:4.8.2'
         extraLibs 'net.sf.ehcache:ehcache:2.8.0'
   extraLibs 'org.slf4j:slf4j-api:1.7.5'
    extraLibs 'javax.ws.rs:javax.ws.rs-api:2.0'
   extraLibs 'org.json:json:20131018'
         }
  // startScripts.classpath += files('$APP_HOME/lib/*')
      version = '1.0'
      jar {
                  into('') {
         from configurations.extraLibs
              }
               manifest {
         version = '1.0.0'
         symbolicName = 'CACHE'
         instruction 'Bundle-Activator', 'com.cache.activator.CacheActivator'
         instruction 'Bundle-Classpath', '.,ehcache-2.8.0.jar,javax.ws.rs-api-2.0.jar,json-20131018.jar,slf4j-api-1.7.5.jar'
       }
    }

the manifest file that gets created is as below -

Manifest-Version: 1.0
Export-Package: com.cache.constants;version="1.0.0",com.cache.service;uses:="net.sf.ehcache,javax.ws.rs";version="1.0.0",com.cache.activator;uses:="com.cache.impl,org.osgi.framework,com.cache.beans";version="1.0.
 0",com.cache.impl;uses:="com.cache.service,net.sf.ehcache";version="1.0.0",com.cache.rest.impl;uses:="com.cache.service,com.cache.impl,org.json,com.cache.beans";version="1.0.0",com.cache.beans;version="1.0.0"
Bundle-Classpath: .,ehcache-2.8.0.jar,javax.ws.rs-api-2.0.jar,json-20131018.jar,slf4j-api-1.7.5.jar
Bundle-Version: 1.0.0
Tool: Bnd-1.50.0
Bundle-Name: RMI-Caching-Service
Bnd-LastModified: 1393141173000
Bundle-Activator: com.cache.activator.CacheActivator
Created-By: 1.7.0_45 (Oracle Corporation)
Bundle-ManifestVersion: 2
Bundle-SymbolicName: CACHE
Import-Package: javax.ws.rs;version="[2.0,3)",net.sf.ehcache;version="
 [2.8,3)",org.json,org.osgi.framework;version="[1.3,2)"

The code may appear a bit separated as I have copied it, the original mainfest file is build correctly.

Please guide.