How to install a bundle on boot in Karaf - osgi

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.

Related

Is it possible to install the karaf server using maven?

I would like to use maven to deploy a complete karaf server including my own features. But so far I am unable to find a way to add the karaf server itself as a dependency. This would save a lot of manual steps, esp. for the other developers which would not have to setup karaf manually.
This would have the added benefit that I do not have to check in the karaf server into the repository in order to have all required parts available from the repository.
Question: is there a way to configure a project using karaf which will setup the karaf server and all required dependencies without manually downloading the files from the karaf website?
This can be done using the karaf-maven-plugin. There is even an example which does this shipped with karaf: examples/karaf-maven-example.
Here is what the examples do
karaf-maven-example-run uses the karaf-maven-plugin:run goal to download and start a Karaf instance.
karaf-maven-example-run-bundle uses the karaf-maven-plugin:run goal to download, start a Karaf instance and deploy the project bundle in this running instance.
karaf-maven-example-deploy uses the karaf-maven-plugin:deploy goal to deploy the module artifact into a Karaf instance (remote for example).
karaf-maven-example-client uses the karaf-maven-plugin:client goal to execute a shell command on a running Karaf instance.
karaf-maven-example-kar packages a features repository as a kar file, ready to be deployed.
karaf-maven-example-assembly uses the karaf-maven-plugin to create a Karaf distribution.
see here for more details.

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>

How to deploy a karaf feature from a local maven repository?

I've created by first project for karaf (4.0.1). So far I've got the following:
an application bundle built with the maven-bundle-plugin. There are going to be more...
a karaf feature created with the karaf-maven-plugin.
a karaf-assembly created with the same plugin
So I'm ready to deliver to the customer - but I'm stuck over the development process. This question answered almost all my remaining questions save one:
After having built my project by running maven install I think I should be able to to get my feature running on a local karaf instance using something like the command feature:install.
I can install my application bundle from local maven by using bundle:install but then I'm down to managing dependencies myself.
Really can't figure out while bundle:install works against my local maven repository and feature:install doesn't.
Found the soloution here. I knew I'd need feature:repo-add - but the name of this command and the official documentation sent me thinking in entirely the wrong direction.
This command does not add a (maven) repository, but adds the location of a feature in the maven repository chain (so I'm thinking the old name addUrl was probably easier to understand).
Anyway - using feature:repo-add mvn:<groupId>/<artifactId>/<version>/xml/features works.
I've just had the similar issue and I've found solution here: http://karaf.922171.n3.nabble.com/Add-additional-local-maven-repository-how-td4028299.html
I've edited the etc/org.ops4j.pax.url.mvn.cfg and uncommented the option org.ops4j.pax.url.mvn.localRepository, setting it to my local repository location:
org.ops4j.pax.url.mvn.localRepository=C:/data/maven-repo
Before installing a feature, you have to register the features repository that provides the feature using feature:repo-add
using feature:repo-add mvn:<groupId>/<artifactId>/<version>/xml

Hawtio as an OSGI bundle

We have our own karaf based application and recently we decided to migrate our web console on hawtio as a plugin. Is there any possibility to append hawtio to our project as an OSGI bundle? i've downloaded hawtio project, but there is no maven module with packaging mode "bundle" or something like that. As an option of course we can get kar archive file, extract it and use its bundles. But that's not a native way at all. Thank you!
Hello have you try to use the latest Hawtio for karaf (hawtio-karaf) available on maven repository?
http://search.maven.org/#artifactdetails|io.hawt|hawtio-karaf|1.4.26|jar
It provide a features.xml that can be install on Karaf.
First: install everything on your local maven repo.
Second: launch your karaf.
Third: run the command: feature:repo-add mvn:io.hawt/hawtio-karaf/1.4.26/xml/features
Fourth: run the command: feature:install hawtio
According to the official website, the default URL is: http://localhost:8181/hawtio/
Let me know if it is working as you were expecting.
There is no clean solution for standalone unit testing, but apache karaf provides exam environment for such situations - http://karaf.apache.org/manual/latest/developers-guide/writing-tests.html
So we can run tests with our application and hawtio in dev mode.
On production this leak of functionality is absent.

How to deploy easily to Karaf Osgi container with maven project

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

Resources