How to work with frequent local snapshot bundle deployments on Karaf?
Asked Answered
T

1

18

I decided to build an application on top of OSGI and Karaf - I really like this stuff. However, I'm struggling a bit with a daily deployment on my local, development machine. I mean.. I make a change and then I would like to test it on my local Karaf instance. And it can happen like couple times per hour.

The way I'm doing it now is a maven build that creates a JAR bundle and then it's copied into the Karaf's deploy directory. I think that it isn't elegant at all.

I was trying to find a way around (google). I read about Karaf's features but it seems that despite the fact that it is a nice mechanism for deploying whole app, it doesn't solve my problem. As I understand it right, it does not check whether new version of my SNAPSHOT jar appeared in my local maven repo, right?

Tiphani answered 16/7, 2014 at 22:32 Comment(5)
Why not run an OSGi framework directly from your IDE?Spannew
Do you suggest Karaf/OSGI framework startup with each application run? Or bundle deployment on Karaf using IDE?Tiphani
I'm suggesting running the bundle in an OSGi framework each time you need to run it. Karaf is an application server, I wouldn't want to deploy to it for development.Spannew
The difficult thing when doing this approach in karaf is recreating an environment in the pure OSGi framework that resembles karaf enough to be useful. Quite often this is too much work to be feasible. (e.g. when you work with jpa in your bundle you need to deploy very much of karaf to make it work)Ezzell
Moreover, application consists of many different bundles that interact with each other. I would like to test how application behaves when one of these needs to be upgraded on fly. This is very important to test if all services and it's dependencies properly behave on upgrade of some parts of application.Tiphani
E
31

The key to make the update mechanism of karaf work is to deploy from maven instead of using the deploy folder. Install you bundle like this:

install -s mvn:groupid/artifactID/version

or

install -s mvn:groupid/artifactID/version/typeOfMavenArtifact

Second one is useful for installing for example war/wab artifacts. Full maven protocol specification can be found here.

Then Karaf knows where the bundle came from. You can also check this using la -u. This makes karaf show the update location which now should be a maven uri. You will not that all karaf bundles have an update location like this.

When you now create a new build of your project using maven it will end up in you local maven repository. Then simply run

update <bundleid>

This makes karaf check the update location (in your case you local maven repo) and reload the bundle from there.

You can even further automate this by using

dev:watch

or for karaf 3+

bundle:watch

This will make karaf check you maven repo for changes in SNAPSHOT bundles it has deployed and automatically redeploy these.

This also works very well together with the remote debugging. Use

export KARAF_DEBUG=true

before starting karaf. It then will listen for a debugger on port 5005.

You can then start a remote debug eclipse session on the same port and nicely debug your application in karaf. This works very well even if you change your bundle using one of the approaches above. So you can debug, find your problem, change the code, build and continue debugging with the changed version.

I also use this frequently when I work at the karaf code base itself as this also works for most of karaf's own bundles.

Ezzell answered 17/7, 2014 at 6:8 Comment(5)
This does not seem to be working for us in Karaf 4.0.4. The update <bundleid> command works once only to update the bundle from our remote maven repository (Nexus). After that, future update commands do nothing, even though we have @snapshotUpdate=always specified. If we restart Karaf, then the next update works (once only).Guillaume
This is only meant for the local repository. You compile the snapshot locally and it will be auto updated. What is your use case with the remote repo?Ezzell
We have DEV servers that we want to deploy snapshot artifacts to. First we build them using our CI server (Bamboo), which places them in Nexus remote repo. Then we want to deploy them using karaf shell's bundle:update command. (Or sometimes using the Update button in karaf web console.) This works for release artifacts, but for snapshots it has the problems I described.Guillaume
Okay, we figured out the way to fix our snapshots-not-downloading-from-remote-repo problem. We had to add a system property of aether.updateCheckManager.sessionState=false. Then it checks the remote repo every time, instead of just once per session.Guillaume
We tried practically everything we could, and realized from logs that the problem had to do with aether component saying it was puposely not updating from remote repo because it had "already updated this session" or something like that. Another developer in my office dug into the source code, and he found that system property that could control the session-based check.Guillaume

© 2022 - 2024 — McMap. All rights reserved.