I want to develop a multi-module application according to OSGi specification. Let's assume that one of my modules uses Apache Commons Logging 1.1.1. Spring provides a bundled version of Apache Commons Logging 1.1.1 in their repository, so I can add the corresponding dependency in my POM.
If I install my bundle in Apache Felix for example, is it right that the dependency to Apache Commons Logging 1.1.1 will not be resolved until I install the bundle of Apache Commons Logging 1.1.1 as well? My bundle will try to import a package that hasn't been exported.
I don't really understand how dependency management works in the OSGi world. Should I install every bundle that my application needs? Also, I don't understand how it integrates with Maven?
Thanks in advance for your explanations
EDIT: I've seen there is a subproject of Apache Felix called OBR that seems to facilitate bundles management (e.g. deployment). But, we already have a Maven architecture with local repositories, private repositories... How is OBR integrated to Maven?
If you want to use OSGi together with maven then I recommend Apache Karaf as a server which can use the Felix OSGi framework. The advantage is that you can install bundles directly from maven repositories using mvn: urls.
If you just use Apache Karaf with a maven repo then you have no transitive resolution at runtime. You have to install all bundles you need. What helps a lot is that karaf has the concept of features. So you can use the features as coarse grained building blocks. You can also create your own features where you refer to other features and bundles. This allows to install your whole app with one command.
Felix and Karaf also support OBR but you would have to create your own OBR Repo. There are currently no public OBR repos. The advantage of OBR is that it can resolve most of the transitive dependencies. The Karaf features even work together with OBR so you can just list some top level bundles in the feature and let OBR resolve the rest.
In practice I have good experiences with simple Karaf features without OBR. It is some manual work but less than you would expect and works quite well.
OSGi dependencies are based on Java packages: when a bundle states that it needs to import a given package (indicating a range of versions that are acceptable), the framework will attempt to "wire" that import to a suitable version of that package, that must be exported by another bundle.
If the package cannot be found, the bundle won't be resolved and cannot be started - so yes you'll need to install all bundles that your application requires, and as Christian indicates there are various tools that can help you with that.
A simple way to get the required bundles is to use the maven-dependencies-plugin to grab the bundles from Maven's list of dependencies and copy them somewhere where your app can find them at startup to install them, as done in [2] (in launcher/pom.xml), a small example app that I wrote.
You could also use Sling's maven-launchpad-plugin [3] to generate a runnable jar file that embeds the OSGi framework and all bundles that you need, and sets up everything at startup.
About the import and exports - without going into details, assuming you use the maven-bundle-plugin [1] to build your bundles, you'll specify what packages you want your bundle to export (others will be invisible to other bundles), and the maven-bundle-plugin will generate (in most cases automatically, but you can override that as needed) the list of packages to import, so there's usually not much work if your own code is cleanly split between packages that you want to export and internal implementation packages.
[1] http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html
[2] https://github.com/bdelacretaz/OSGi-for-mere-mortals
[3] http://sling.apache.org/site/maven-launchpad-plugin.html
Related
I'm struggling to wrap my head around developing OSGI bundles to publish to Karaf. I'm using Netbeans 7.4 and the Create Maven OSGI bundle when creating a new project. I think what I'm wrestling with is dealing with dependencies between different projects and working with them in Netbeans and then publishing to Karaf.
What I'm trying to do is set up a REST web service; I found this tutorial to be a good starting point in getting something basic up and running. So I started setting up a parent project with sub projects (all of these are from the Maven OSGI Bundle template). But after I started deploying bundles to my Karaf instance I started wrestling with the "missing requirement" errors within Karaf. I'd start trying to list each dependency in <Export-Package> or <Embed-Dependency> but it seems like I just keep going farther down and always getting new "missing requirement" messages listing yet another dependency. I then started trying the maven copy-dependencies plugin to export whatever dependencies are used in my project and copying whatever comes out into the Karaf deploy folder, too. That worked for the most part but I'm wrestling with what I think are downstream dependencies of a 3rd party jar, but that's probably a separate question anyway.
So, what I'd like to know is how should I be developing a coherent "application" with this setup? I understand I should separate everything into standalone bundles; i.e., I'll have my REST interface as one, the implementation as another, business logic as another. As a maven project, if I have a dependency for datetime handling or string utilities, maven will handle resolving all of those. But these dependencies won't automatically be included in my OSGI bundle, is that the case? What is the workflow to make sure that everything my project depends on is available to the bundles in Karaf? What about my projects that have other projects as dependencies, i.e., an interface implementation? I would include that project as a dependency so it will compile, but how do I make sure that that dependency is met after it is published?
The mismatch between dependencies at compile time and at deployment time is indeed a problem when working with OSGi.
What you need to know is how these relate to each other. At compile time you have the maven dependencies. When you build your project the maven bundle plugin creates the necessary Import-Package and Export-Package statements for you. Most of the time this simply works and you should avoid tuning too much there by hand.
So after the build the Manifest specifies what the resulting bundle needs but this does not automatically make sure the dependencies are met. So when you deploy the bundle to karaf you get the errors about missing dependencies. This is normal.
Now you need to install other bundles that fulfil these dependencies. Often you can simply install the jars of your maven dependencies (if they are bundles). This does not always work though. So basically you install the bundles and check if the requirements are met now.
The nice thing with karaf is that many bigger dependencies like cxf are already available as features. So it is a good idea to first try to get the dependencies by installing available features before you try to install them bundle by bundle.
So this helps you in getting your dependencies right. Then for "production" use the best solution is to create your own feature file where you refer to the bundles you found.
While installing bundles using the deploy dir seems nice at first it is not a good solution. Better install the bundles directly from maven using the mvn: url syntax karaf provides.
Regarding embedding dependencies. Sometimes it is a good solution but often it only makes things worse as it can lead to package use conflicts which are hard to solve. So better do not embed anything if it is possible.
I am running Apache servicemix 4.5.2. I want to install a feature, i.e. a jar file.
The feature I wanted is jtidy.
The pom dependence is:
<dependency>
<groupId>jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>4aug2000r7-dev</version>
</dependency>
and the repository is
http://repo1.maven.org/maven2/jtidy/jtidy/4aug2000r7-dev/jtidy-4aug2000r7-dev.jar
I know the command features:install webconsole, for example but jtidy is not in my features:list. I've also tried using the addurl command but it didn't work.
(addurl mvn:http://repo1.maven.org/maven2/jtidy/jtidy/4aug2000r7-dev)
The Karaf documentation recommends to add a feature descriptor using the Features XML schema but unfortunately the link is broken.
Up to know what i did is to download the jtidy.jar and copied it to my deploy directory. It works, but I don't think that this is the correct way.
Do anybody knows how to install jtidy in servicemix correctly?
Thanks!
There's difference between installing a feature and installing a single JAR or OSGi bundle.
A feature is defined in an XML file. A feature consists of a number of bundles, configs, ... that are installed together. Have a look at http://karaf.apache.org/manual/latest-2.3.x/users-guide/provisioning.html to learn more about features in Karaf.
In this case, you want to install a single JAR into the container. You can use the command osgi:install to do this, followed by a URL pointing to the JAR (e.g. mvn:jtidy/jtidy/4aug2000r7-dev).
However, in your case, there's one more complexity. jtidy is not an OSGi bundle by itself. The easiest way to add the necessary OSGi metadata, would be to use the wrap: protocol to automatically add the OSGi metadata to the JAR.
So, to wrap things up - to install this jtidy dependency in Apache ServiceMix, you can use
osgi:install wrap:mvn:jtidy/jtidy/4aug2000r7-dev
I'm new in OSGI development and am struggling to understand how best to handle dependent JARs.
i.e. if I'm creating a bundle the likelihood is that I will need to use a few 3rd party JARs. When I create my bundle JAR to deploy to OSGI, obviously these 3rd party JARs are not included and thus the bundle will not run.
I understand that one option is to turn these JARs into bundles and deploy them to the OSGI container. When I bundled and try deploy third party jar it throws error for their own dependent jars and these dependencies are endless and not possible to fulfill.
What is the best solution to this?
-- Anurag
Finally I resolve third party jar issue actually I forger to add following tag in my POM.xml file
<_exportcontents>*
My issue is resolve, thanks to all of you for your support and giving me valuable clues.
If you are a beginner with osgi then I would not recommend to bundle bigger libs yourself. Often more than defining some import and export headers is required and it can become very difficult. Fortunately a lot of libs are available as bundles.
If the original lobs is not yet a bundle then you can search in maven central for a bundled version from servicemix bundles.
Using apache karaf is also often a big help. There are karaf features with predefined deps for a lot of libs like activemq, cxf, camel, openjpa, ...
In the worst case you can embed the lib and all deps in your own jar. This tends to cause class loading issues though if you try to share some classes between bundles.
What lib is the problem in your case?
A lot of libraries you mention (log4j, quartz, dom4j) have already been converted to OSGi bundles by Springsource. You can find them in the Springsource Enterprise Bundle Repository. All Hibernate libraries > 4.2 are also osgified.
You have three choices:
Find the library in some pre-made bundles (Springsource Repository, Eclipse Orbit Repository, etc.)
Include the libraries into your bundle as an external library (Make a directory lib, copy your jar files there, and reference them from the Bundle-Classpath element in MANIFEST.MF)
Convert the library into an OSGi bundle (e.g. with the bnd wrapconverter, example here)
According to this post, IDEA uses Osmorc to run OSGi frameworks. It, in turn, uses Pax Runner to launch different framework implementations.
The toolchain in IDEA 11 can only run Apache Felix up to 3.0.2, but I have to run version 4.0.2. Is it possible? Do other OSGi framework launchers exist for IDEA?
You can configure the OSGI frameworks that Osmorc uses from the plugin+ project settings.
I use IntelliJ IDEA 11.1.2 and Osmorc 1.4.2.
Here is a screenshot where I configure the OSGI container (at IDE Settings-->OSGi) to some local
installation of Apache Felix 4.0.1.
You can also define your custom framework/container and then select the desired
OSGI framework to use at Project Settings-->OSGI as seen below:
In order to create Run configurations, you first need to create some OSGi facets.
Hopefully, you are also using maven and the maven-bundle-plugin which will greatly
reduce the amount of configuration you need to do(since OSmorc automatically syncs with your pom.xml), but even if you do not, you can manually edit the information for creating the bundles.
So, what you need to do next is create some OSGi facets. Go to Project "Structure-->Facets"
and add a new OSGi facet for each bundle you wish to create. It you have automatic detection turned on, then the facets maybe already there. If not, then add them manually and then configure them as you see appropriate, like in the following example.
Finally edit your "Run Configurations" and add a new OSGi run configuration.
Select the framework you wish to use and the bundles you wish to start as well as
other parameters, like in the following example:
I have a java project that compiles down to an OSGi bundle. I'm using the maven-bundle-plugin to create the bundle, and the maven-sling-plugin to push the bundle to my running OSGi server. At the moment, my project has two dependencies (Guava and Gson) that need to be manually installed in the Felix server prior to installing the bundle. The manual install for these two dependency bundles only needs to occur once (when first setting up the OSGi server), but since it's a manual process, I'd kind of like to just push it into the "mvn install" phase.
I've looked at a number of maven plugins (maven-sling-plugin, maven-ipojo-plugin, dependency plugins, etc.), but I'm afraid that I just don't have enough knowledge to know where to start, or even to search in Stackoverflow for the solution (I have a suspicion that this has already been answered).
So - is there a clean way to install/start dependent bundles with maven? Something where I can specify a jar and a runlevel, and have the installation take place prior to my bundle being installed?
The Ops4j pax construct project might help: http://team.ops4j.org/wiki/display/paxconstruct/Pax+Construct
Looks like it's possible to use maven to specify which bundles to install and then spin up a felix osgi environment as described here:
http://www.sonatype.com/books/mcookbook/reference/ch01s04.html