How to deploy easily to Karaf Osgi container with maven project - maven

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.

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

Related

How to deploy a Apache Camel Spring boot project in servicemix?

I have a Spring Boot Apache Camel project. And now I wanted to deploy the same in servicemix. Can some one help me with the steps in deployment of the package which is generated as part of mvn goal.
From Camel examples I was able to get the answer. And the following is the example which helped me:
https://github.com/apache/camel/tree/master/examples/camel-example-osgi
But, the problem with this approach would be I will be need to publish my code to maven repository and installation will be happening from there. And the machine in which I run service mix does not have maven(we are not supposed to install maven)
You can deploy features or bundles from the file system using the file:// protocol as shown in this documentation: Servicemix provisioning
"Deploy bundles from file system without using Maven:
As we can use file:// as protocol handler to deploy bundles, you can use the following syntax to deploy bundles when they are
located in a directory which is not available using Maven"
<features xmlns="http://karaf.apache.org/xmlns/features/v1.0.0">
<feature name="spring-web" version="2.5.6.SEC01">
<bundle>file:base/bundles/spring-web-2.5.6.SEC01.jar</bundle>
</feature>
</features>

JBoss Fuse vs. standard Maven dependency

I'm just learning OSGi, JBoss Fuse (6.1) and Karaf. How can I use a standard (not bundle) Maven dependencies without changing them?
I have a simple Maven bundle project. It depends on some third party libraries. It uses them via its blueprint.xml . I understand if the manifest.mf marks packages in the Import-Package entry then there have to be installed bundle with Export-Package in its manifest.mf. In my case if I install these standard Maven projects they won't export the required packages. So I've got the "Unresolved constraint in bundle" error message during the installation. Is the Maven Bundle or Shade plugin able to solve this issue? Or if they can't which is the most elegant way to resolve the dependencies?
Somewhere I've read the Jboss Fuse is able to resolve dependencies from the Maven repository. Can I use this mechanism somehow?
Thank you!
This may be a bit late now, but you can do that using features.
<features>
<feature name="wrap_features" version="1.0">
<bundle>mvn:org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1</bundle>
<bundle>wrap:mvn:org.jdbi/jdbi/2.70</bundle>
<bundle>wrap:mvn:com.microsoft/sqljdbc4/4.2</bundle>
<bundle>wrap:mvn:org.springframework/spring-web/4.2.4.RELEASE</bundle>
<bundle>wrap:mvn:org.springframework.security/spring-security-core/4.0.3.RELEASE</bundle>
<bundle>mvn:com.doi.ws/mssql-fragment/1.0.0</bundle>
<bundle>mvn:com.doi.ws/mssql-impl/1.0.0</bundle>
<bundle>mvn:com.doi.ws/doi-services/1.0</bundle>
</feature>
</features>
You install the features before deploying your project to the fuse server using the features command
Meanwhile I found an almost good solution: with wrap: namespace/prefix Fuse automatically generates a bundle during installation:
osgi:install -s wrap:mvn:cglib/cglib/2.2.2
But I still have to install each dependency manually. Is there any way to automatize the installation of dependencies?
Thx!
Use Bundle-ClassPath manifest header.
Make sure all your maven dependencies are available in the final jar file under a single directory called 'lib'.( This can be achieved using maven resources plugin that copies maven dependencies in any output directory).
Use maven-bundle-plugin and customize the bundle manifest Bundle-ClassPath attribute with its element. Add a path to lib folder so that all the plain vanilla jars are available as a part of bundle classpath.

How to install a bundle on boot in Karaf

I came across this post but it doesn't quite answer my question. I am using blueprint to set up a H2 database in Karaf and it requires that I first install H2 driver and OSGi enterprise package because it contains JDBC library.
install -s mvn:org.osgi/org.osgi.enterprise/4.2.0
install -s mvn:com.h2database/h2/1.3.174
I added the blueprint XML file to Karaf's deploy folder and it keeps giving error messages until I install H2 and enterprise package. Ideally, I would like to install both enterprise and H2 before the blueprint script kicks in, so I am thinking somehow add it to karaf's boot process but I am not sure how.
Any insights will be much appreciated.
One option would be to hot deploy a features XML before deploying your bundle.
See the Karaf deployer guide for more details. Here is an XML example:
<features>
<feature name="features_test">
<bundle>mvn:org.osgi/org.osgi.enterprise/4.2.0</bundle>
<bundle>mvn:com.h2database/h2/1.3.174</bundle>
</feature>
</features>
One way to achieve this is to publish the blueprint file to the maven repo. This way you can reference it in feature files. See the maven build helper plugin with goal attach artifacts. Many feature files are deployed tnis way.
You can also put your blueprint file into an ordinary bundle. Then it will also work in other containers than karaf.

how to make camel-example-cxf-proxy work in serviceMix 4.3.0

I downloaded camel 2.6.0 and used example camel-example-cxf-proxy (under example folder) to deploy into serviceMix.
what i would like to do is packaging it to become a jar file and then goto serviceMix karaf console to install this jar.
(install -s wrap:file:/c:/...jar)
But this example doesn't work in serviceMix 4.3.0.
Do you know what thing i have to modify to make it work within serviceMix 4.3.0
Do i have to install any other bundle into serviceMix?
The first thing is that you should make sure your maven project builds a bundle. I recently did a tutorial that shows how to run camel routes in OSGi:
http://www.liquid-reality.de/display/liquid/2012/01/03/Karaf+Tutorial+Part+5+-+Running+Apache+Camel+integrations+in+OSGi
So basically you have to make sure that the camel features you need are installed. In your case this is probably camel-cxf and camel-http.
Then you need to change the pom to create a bundle. See the pom of the example:
https://github.com/cschneider/Karaf-Tutorial/blob/master/camel/jms2rest/pom.xml
You need to change the packaging to bundle and add the maven bundle plugin. For camel projects the default typically work very well.
Btw. you can also run camel on pure karaf when you follow the instructions in my tutorial. This has the advantage that you can almost always use the newest camel version while servicemix often comes with an older version.

Karaf development

Im currently develop bundles for karaf and have some questions...
I wrote a bundle/webservice based on cxf, I try to deploy it in karaf but it could not start that bundle because it could not resolve some packages e.g.
org.osgi.framework.BundleException: Unresolved constraint in bundle org.springframework.aop [56]: Unable to resolve 56.0: missing
requirement [56.0] package; (&(package=org.aopalliance.aop)(version>=1.0.0)(!(version>=2.0.0)))
so here is a question, this package dependency comes from spring-aop (3.1.0.RELEASE), so where is the problem? what dependency is missing? how can I solve such problems?
In that case i did not clearly understand the development process. should i deploy all missing bundles in deploy? because i would like to keep thirdparty libs spereated from my developed bundles. And what bundles i have to deploy? Is it a trial and error process? Is there a common way to let maven do the dependency stuff?
I discovered a folder "system" and read on the docu that it is a repository like maven, is it for the features?
I had for test cases a karaf with some pre deployed bundles and put my webservice bundle into it, but again execeptions...
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
What dependency is missing?
I already read the tutorial about camel and karaf, but it did not explain the deployment stuff, so could anyone suggest me a good tutorial?
Thanks!
Chris
Short answer
Scroll down to the bit referring to "camel-cxf" and run the two commands features:addurl and features:install. I have a feeling this will resolve all your problems.
spring-aop
On Karaf console type:
exports | grep org.aopalliance.aop
I think you'll see lines like:
XX org.aopalliance.aop; version=3.1.0.RELEASE
So while the spring-aop bundle has the right packages they're the wrong version, the range being requested is >=1.0.0 and <2.0.0, so 3.1.0 doesn't satisfy that.
Deploying/Installing
You can drop bundles into ${karaf.home}/deploy or use the console.
You can install maven bundles from the Karaf console with:
install -s mvn:groupId/artifactId/version/packaging/classifier
Where -s starts the bundle and packaging/classifier are optional.
You can find a lot of OSGi ready maven dependencies here http://ebr.springsource.com/repository/app/ - I had a quick look but your spring aop dependency is very old, what version of CXF are you using?
Read up about Karaf features - they're basically XML files that list suites of bundles that can be installed. Very useful for deploying large numbers of bundles and they can be installed into a maven repository.
There are some standard features available in Karaf, try:
features:install war
This will give you a jetty webcontainer and may resolve your ClassNotFoundException: javax.servlet.http.HttpServlet as long as it's the right version
Camel also has a features file which probably sort all your issues, try this:
features:addurl mvn:org.apache.camel.karaf/apache-camel/2.9.0/xml/features
features:install camel-cxf
Tutorials
There's quite a bit available, some on http://karaf.apache.org and http://fusesource.com but also take a look at the PDF manual that comes in the Karaf distribution.
Always beware that info may be out-of-date
Please post your MANIFEST.MF file. I think you didn't not mention the tag in maven-bundle-plugin dependency.

Resources