karaf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steinar Bang ...@dod.no>
Subject Re: Developing for karaf
Date Tue, 05 Sep 2017 14:57:34 GMT
>>>>> tom@quarendon.net:

> 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! :-)


Mime
View raw message