Developing for karaf

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Developing for karaf

tomq42
I'm trying to get an idea of how people go about developing for karaf, from a practical point of view.

So karaf is maven focused. There are archetypes for creating command, general bundles and so on.
I can then use maven to generate some eclipse project files that allow me to write and compile my code within eclipse. I guess if I need extra dependencies, I have to edit my pom and hopefully eclipse picks this up (never really done serious maven development, so I don't know how this process really works).

When I want to try something out, I have to perform a maven build, start up a copy of karaf, install the bundle (or bundles) into it, then try out my new code? All from the command line?
What about debugging? You start karaf with the "debug" option and then remotely connect eclipse to the karaf instance so that you can then place breakpoints and step through the code if necessary? Does it just magically find all the source code?

Just trying to get a picture of what the expected workflow is and whether I'm missing anything. We're used to doing things in bndtools where you've got eclipse tooling for everything, so I'm just trying to do a mental reset really on what a "karaf/maven centric" development environment and process would look like. (I'm aware of the "Integrate Apache Karaf and Bnd toolchain" article, but we've had limited success with it beyond simple "hello world" examples. Maybe we just need more perseverance).

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Developing for karaf

jbonofre
Hi Tom,

You can also create your own Karaf custom distro.

We are also discussing about Karaf Boot to simplify the bootstrapping/ramp up on
Karaf. Short term, we are working on an improved dev guide.

Back on your question:
1. From a dev perspective, you can have a running karaf instance, you just do
mvn install on your bundle, and thanks to bundle:watch, it's automatically
updated in Karaf (not need to perform any command).
2. For debugging, you are right: just start karaf in debug mode (bin/karaf
debug), it binds port 5005 by default, and then, connect your IDE remotely.

Regards
JB

On 09/05/2017 01:48 PM, [hidden email] wrote:

> I'm trying to get an idea of how people go about developing for karaf, from a practical point of view.
>
> So karaf is maven focused. There are archetypes for creating command, general bundles and so on.
> I can then use maven to generate some eclipse project files that allow me to write and compile my code within eclipse. I guess if I need extra dependencies, I have to edit my pom and hopefully eclipse picks this up (never really done serious maven development, so I don't know how this process really works).
>
> When I want to try something out, I have to perform a maven build, start up a copy of karaf, install the bundle (or bundles) into it, then try out my new code? All from the command line?
> What about debugging? You start karaf with the "debug" option and then remotely connect eclipse to the karaf instance so that you can then place breakpoints and step through the code if necessary? Does it just magically find all the source code?
>
> Just trying to get a picture of what the expected workflow is and whether I'm missing anything. We're used to doing things in bndtools where you've got eclipse tooling for everything, so I'm just trying to do a mental reset really on what a "karaf/maven centric" development environment and process would look like. (I'm aware of the "Integrate Apache Karaf and Bnd toolchain" article, but we've had limited success with it beyond simple "hello world" examples. Maybe we just need more perseverance).
>
> Thanks.
>

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: Developing for karaf

tomq42
OK, thanks.

I'm really actually quite passionate about this whole area. The OSGi community is too small to fragment, and there's an excellent tool in the shape of bndtools (gradle based), but it doesn't "deploy" to anything. It's just super frustrating that there's no clear integration path between that and karaf (maven based). There would be an awesome environment if that could be nailed, so that it operated a bit like developing j2ee webapps for a container like tomcat, which seems roughly analogous (integration of running karaf within eclipse itself, automatic deployment of the bundles into the container and so forth, along with the ease of build and dependency configurations that bndtools has).

Anyway. I'll experiment with a "pure karaf" environment and see where it gets me. You've confirmed my basic understanding of the current situation anyhow.

> On 05 September 2017 at 13:07 Jean-Baptiste Onofré <[hidden email]> wrote:
>
>
> Hi Tom,
>
> You can also create your own Karaf custom distro.
>
> We are also discussing about Karaf Boot to simplify the bootstrapping/ramp up on
> Karaf. Short term, we are working on an improved dev guide.
>
> Back on your question:
> 1. From a dev perspective, you can have a running karaf instance, you just do
> mvn install on your bundle, and thanks to bundle:watch, it's automatically
> updated in Karaf (not need to perform any command).
> 2. For debugging, you are right: just start karaf in debug mode (bin/karaf
> debug), it binds port 5005 by default, and then, connect your IDE remotely.
>
> Regards
> JB
>
> On 09/05/2017 01:48 PM, [hidden email] wrote:
> > I'm trying to get an idea of how people go about developing for karaf, from a practical point of view.
> >
> > So karaf is maven focused. There are archetypes for creating command, general bundles and so on.
> > I can then use maven to generate some eclipse project files that allow me to write and compile my code within eclipse. I guess if I need extra dependencies, I have to edit my pom and hopefully eclipse picks this up (never really done serious maven development, so I don't know how this process really works).
> >
> > When I want to try something out, I have to perform a maven build, start up a copy of karaf, install the bundle (or bundles) into it, then try out my new code? All from the command line?
> > What about debugging? You start karaf with the "debug" option and then remotely connect eclipse to the karaf instance so that you can then place breakpoints and step through the code if necessary? Does it just magically find all the source code?
> >
> > Just trying to get a picture of what the expected workflow is and whether I'm missing anything. We're used to doing things in bndtools where you've got eclipse tooling for everything, so I'm just trying to do a mental reset really on what a "karaf/maven centric" development environment and process would look like. (I'm aware of the "Integrate Apache Karaf and Bnd toolchain" article, but we've had limited success with it beyond simple "hello world" examples. Maybe we just need more perseverance).
> >
> > Thanks.
> >
>
> --
> Jean-Baptiste Onofré
> [hidden email]
> http://blog.nanthrax.net
> Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: Developing for karaf

Steinar Bang
In reply to this post by tomq42
>>>>> [hidden email]:

> When I want to try something out, I have to perform a maven build,
> start up a copy of karaf, install the bundle (or bundles) into it,
> then try out my new code? All from the command line?

If by "the command line" you mean "the karaf console", then yes.

The installation of a bundle is like this:
 bundle:install mvn:no.priv.bang.ukelonn/ukelonn.bundle/1.0.0-SNAPSHOT/war
(ie. an URL consisting of the maven groupId/artifactId/version/type)

Karaf will then scan through its list of maven repositories and try to
find it.  The first place it will look is the "local maven repository",
where maven puts its artifacts when it builds, ie
 $HOME/.m2/repository/

This repository also have a special place when it comes to watching for
changes, see more below.

It is, however, better to use something called karaf features instead of
installing the bundles directly.  More on that as well below.

> What about debugging? You start karaf with the "debug" option and then
> remotely connect eclipse to the karaf instance so that you can then
> place breakpoints and step through the code if necessary? Does it just
> magically find all the source code?

Eclipse finds source that are added as source attachements to maven
dependencies automaticially.

For source in the projects in eclipse, I have to edit the eclipse run
configuration and add the projects to the Source tab, basically
 1. Open from the menu Run->Debug->Debug Configurations...
 2. Select the run configuration for attaching to to karaf
 3. Select the "Source" tab
 4. Click the "Add..." button and
    a. Select "Java project"
    b. Click "OK"
       i. select all the projects you wish to scan for source
       ii. Click "OK"

Then back to the stuff I promised to come back to.

First: Watching the local repository

If you on the karaf console do the following command after loading your
OSGi bundles (either directly or via a feature):
 bundle:watch *
then karaf will start watching your local repository for updates to all
the loaded bundles with "-SNAPSHOT" in their version number.

This means that if you do
 mvn clean install
in your project the bundle will be rebuilt and karaf will reload the
rebuilt bundles.

Note: The m2e plugin of eclipse that handles maven support in eclipes
will not rebuild the bundles in the manner required by karaf.

To get something karaf picks up, you will either have to build from the
command line,

Your you will have to do I like I do: create a maven run configuration
in eclipse, and just press
 Alt-Shift-X m
(ie. first press Alt, Shift and "x" simultanously, then just press "m").

Then maven will rebuild the bundle(s) and karaf will reload it/them.


A little side note on the maven build command:

The default build command is
 mvn clean install

This is a safe command, but it always tears down the entire world.  It
deletes all artifacts from the previous build and then run all tests
and, if configured to do so, generates javadoc and packages source jars
(the ones eclipse uses to find the source when debugging maven
dependencies).

Just doing "mvn install" (ie. without the "clean") is often enough, and
will be faster.

You can also skip the tests and extras such as javadoc generation and
source jar packaging.  This is the cheapest maven build command, and
what I run when I wish to have a quick turnaround (e.g. when getting
fresh code up and running in karaf):
 mvn install -DskipTests=true  -DskipSource=true -Dmaven.javadoc.skip=true


Then back to karaf features:

Karaf features is a way to describe the dependencies of your bundle.  

A karaf feature is an XML file that is associated with your bundle and
lists the dependencies of the bundle.  The dependencies can be bundles
(identified with mvn URLs) and other features.

What I do, is:
 1. Piggy-back a feature on each bundle project
 2. Aggregate all of the features into a large feature file called a
    "feature repository"


Here is an example of a feature piggybacked on a maven bundle:
 - The parent pom configuration contains the actual shared configuration
   in the <pluginManagement> section
    https://github.com/steinarb/ukelonn/blob/master/pom.xml#L296
 - The pom configuration just references the plugin in the <plugins>
   section. Iit inherits the version and all of the configuration from
   the <pluginConfiguration> in the parent (and replaces/adds the
   setting <primaryFeature>)
    https://github.com/steinarb/ukelonn/blob/master/ukelonn.bundle/pom.xml#L226
 - The karaf-maven-plugin uses a template file for the feature, in
   addition to the bundles in this file, it will add bundle dependencies
   to all project dependencies with scope <compile>
    https://github.com/steinarb/ukelonn/blob/master/ukelonn.bundle/src/main/feature/feature.xml
   (Note: all of the "pax-" features are built-in in karaf)
 - The resulting feature file, as deployed to a maven repository, looks
   like this
    https://maven.bang.priv.no/repository/no/priv/bang/ukelonn/ukelonn.bundle/1.0.0-SNAPSHOT/ukelonn.bundle-1.0.0-20170831.154207-3-features.xml
   (Note: may be deleted if I start pruning the snapshots of this
   repository. Try the directory level if so)

As for the aggregate feature, the feature repository, the project is
this one:
 https://github.com/steinarb/ukelonn/blob/master/ukelonn.karaf/pom.xml

With this setup I can clone my project, build it with "mvn clean
install" and then install the webapp of the project by giving the
following commands at the karaf console:
 1. First install the "feature repository" created by the aggregate
    feature:
     feature:repo-add mvn:no.priv.bang.ukelonn/ukelonn.karaf/LATEST/xml/features
 2. Then I install the features that will pull in all of the
    dependencies as needed:
     feature:install ukelonn-db-derby-test
     feature:install ukelonn
 3. Then I tell karaf to watch for, and pick up rebuilds to the
    -SNAPSHOT versioned, locally built bundles:
      bundle:watch *

Then I open a web browser to http://localhost:8181/ukelonn/ and start
testing (username admin, password admin if you're trying out this
project).

To debug I can attach a remote debug session from eclipse.

Good luck! :-)