automated osgi bundle development with netbeans 7.1 - java

I'm currently developing a maven osgi bundle using Netbeans 7.1. While its easy to create new maven osgi bundle project from netbeans, I've been struggling about how I would run it. Simply running it from netbeans gives me an error that tells me there is missing requirement (missing slf4j for example).
Then I tried a different way. I ran equinox in a terminal and then manually install my project jar to it. But then I must also resolve all the dependencies manually.
Is there any way to automatically download all the required dependencies from an OSGI bundle and install it to a running OSGI framework?
thanx before

AFAIK there's no automation for OSGi in Netbeans (though I may well be wrong). The support you may have seen in Eclipse is only for Eclipse plugins not vanilla OSGi.
Your best bet is to go for some integrating testing or launch via a maven goal, pax-exam or bndtools or even pax-runner will allow you to launch from IDE/maven.
However I don't know of anything that will automatically resolve all dependencies (transitive dependencies and implementations of APIs you depend on would be problematic)

Its a big pain, no question about it. What I used to do was to do a full build (and if you've configured your manifests correctly should include all necessary dependencies) which will generate the necessary jar. I then wired my Tomcat to pick up the jar from my target repository and configured it to hotswap automatically.
Its a matter of preference if you want to run your app server from within your netbeans but I preferred to execute a separate instance of tomcat outside of my IDE. It'll work either way tho.

Related

How to manage dependencies when developing OSGI bundle in Netbeans and Karaf?

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.

Eclipse and maven dependency during development

I do have own developed Java library (MyLib), which I later publish on private Maven repository and have it as Maven dependency in another webapp project (MyWebapp). If I have both projects - MyLib and MyWebapp at the same time opened in Eclipse - is there a way somehow to configure MyWebapp so, that local changes made to MyLib would be directly added to MyWebapp while building/deploying it? The issue is that during development it is not really comfortable always to make some changes in MyLib, make a build, deploy to Maven repository and then make a MyWebapp build, deploy it and only then I can see how the changes are affecting the webapp project...
I would like to reduce the overhead while developing and willing to see how changes are working out. Of course when it comes to real releases the above described flow does really make sense and works great.
Thanks!
There is no simple "just tick this option" solution, unfortunately. You can chose between these options:
Convert MyLib into a Maven module and add it to the sources of MyWebapp. This, of course, will make it harder later to reuse the library alone.
Stop deploying the application. If you look at the classpath in Eclipse, m2e should have added the dependency as a project from the workspace (instead of depending on the JAR in the repository). If not: There is an option for this.
The next step is to create another project which depends on Jetty and MyWebapp. Create a Java application in there (i.e. a file with main()) which creates a Jetty server and configure it to use the current classpath. That way, you can start the webapp just like any other Java application without deploying - Jetty will simply load classes from the classpath that m2e assembled.

Run Apache Felix 4.0.2 in IntelliJ IDEA 11

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:

Installing OSGi dependencies with maven-bundle-plugin

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

Export OSGI-Application from Eclipse

I've developed an osgi application (no rcp) using eclipse. It consists of several Plugin-Projects-Bundles and dependencies from eclipse plugins folder (commons.* ...)
The application works fine when launched using eclipse.
What is the best way to export and deploy such an application from eclipse? Is there a simple way to export my launch configuration?
All I found was for rcp projects.
All you need to do is religiously fill your Manifest.MF via PDE(Plugin Editor), you must
Add proper plugin-dependencies in the Dependecies tab in PDE
Fill the Classpath and Exported Packages in the Runtime tab
And most importantly, make sure in the Build tab you have checked on the required resources to be exported.
Its very important to note point 3, it is here where most people make mistake and wonder why the project is running perfectly in Eclipse but doesent run when exported.
Right-Click on your
project->Export->Plugin-Development->Deployable
Plugins and Fragments
Check out Chapter 9 on packaging OSGi/Equinox applications in the new OSGi and Equinox book. It's available on rough cuts now: http://my.safaribooksonline.com/9780321561510. It should be available in print for purchase by EclipseCon in March.
Creating an OSGi bundle, by itself, does not constitute a complete application. OSGi bundles require a container and its the container's responsibility to manage the lifetime of the bundle: loading the bundle, resolving dependencies, invoking the bundle's activator, etc. There are several OSGi containers available such as Knopflerfish (http://www.knopflerfish.org/), Felix (http://felix.apache.org/), and Equinox (http://www.eclipse.org/equinox/). Internally, Eclipse uses Equinox.
Deploying an application that uses OSGi entails configuring the container and the exact mechanism for doing that depends on the chosen container. If you wish to continue using Equinox then check out this quick-start guide for configuring and launching the container outside Eclipse (http://www.eclipse.org/equinox/documents/quickstart.php).
PDE generates a configuration that can be customized much easier that writing an Equinox config from scratch. In your running Eclipse-based OSGi environment, type bundles. You'll see where Equinox is putting all the runtime bundles for the launch. In there should be a config.ini that PDE is generating for the launch. In my case it's [workspace root]/.metadata/.plugins/org.eclipse.pde.core/[My Launch Config Name]/config.ini.
-> Create a feature project in Eclipse
-> Open feature.xml file of the created project.
-> Add all the required Plug-ins and dependencies under "Included Plug-ins"
-> Ensure that you have added all OSGI dependant "Plug-ins"
Here is a list of Plug-in I am using
-> Now create the OSGI run configuration
-> Under "Bundles", select your feature project.
Now all the plug-ins can be exported from the Overview tab of your feature.xml

Categories

Resources