How to deploy easily to Karaf Osgi container with maven project
Asked Answered
C

1

9

I'm developing an OSGI bundle for parsing a PDF file using PDFBox library. I use maven to build the project and Karaf as the OSGI container. The PDFBox library is OSGI compatible so I thought this would be easy. But I just can't get the deployment model right.

In a traditional web app I would build a single WAR-file containing all the dependencies and put it in a Servlet container and it would get deployed. On the other hand the only way I've figured how to install an osgi bundle is by doing it by hand. I have to create an installation instruction file that lists all the dependencies that have to be manually downloaded and copied to the Karaf deploy folder, and be sure to do it in the right order. I feel like I'm back in the stone ages.

There has got to be an easier way, right? I still use maven to declare dependencies but I just have to use the provided scope. It would be great if those dependencies could be automatically installed.

I'm using the maven-bundle-plugin to generate a bundle from my application. It does generate an OBR repository(repository.xml) and I tried installing my bundle using obr karaf plugin but it still doesn't help with dependencies.

Cozy answered 7/3, 2014 at 15:50 Comment(0)
O
9

There are different possibilities for provisioning bundles. I prefer to install a bundle using Maven via the Karaf console such as:

install mvn:org.apache.pdfbox/pdfbox/1.8.4

If you don't want to install every bundle one by one, you could use so called features as described here. A feature lists all needed bundles:

<feature name='my-project' version='1.0.0'>
    <feature version='2.4.0'>camel-spring</feature>
    <bundle start-level='80' start='false'>mvn:com.mycompany.myproject/myproject-dao</bundle>    
    <bundle start-level='85' start='false'>mvn:com.mycompany.myproject/myproject-service</bundle>
    <bundle start-level='85' start='false'>mvn:com.mycompany.myproject/myproject-camel-routing</bundle>
</feature> 

You add a feature via Karaf console:

features:addUrl mvn:org.apache.servicemix.nmr/apache-servicemix-nmr/1.0.0-m2/xml/features
features:install nmr

Instead of the mvn handler, you could also use the file handler:

features:addUrl file:base/features/features.xml
Ottie answered 7/3, 2014 at 16:39 Comment(5)
How do I create that features.xml file? By hand?Cozy
Yes. At least I don't know if there is another possibility.Ottie
The Karaf Maven Plugin does help in building a feature file from dependencies, though it's usually better to fine-tune those features manually.Suazo
I fully agree with Peter and Achim. Feature files are the best way to deploy applications in Karaf. I also build the feature files by hand. Typically you refer to existing features like from camel or cxf. So most of the time the feature files stay small.Malvinamalvino
I investigated the karaf maven plugin and it does seem like you end up in less trouble when you create the features file by hand. Though it does have an interesting feature where it can build kar-archive which includes the maven dependencies that it can resolve from the features.xml file. It's useful when you can't download the dependencies on the production server.Cozy

© 2022 - 2024 — McMap. All rights reserved.