Skip to content

Karaf features primer

Alejandro Medrano edited this page Mar 17, 2018 · 1 revision

Karaf provides a simple way to provision applications by means of features. Such a mechanism is mainly provided by a set of commands.

The provisioning system uses xml repositories that define a list of features elements, each one representing an application that can be installed. The feature is identified by its name which must be unique amongst all the repositories used and consists of a set of bundles that need to be installed along with some optional dependencies on other features and some optional configurations.

An example of a feature would be as follow:

    <?xml version="1.0" encoding="UTF-8"?> 
    <features xmlns="http://karaf.apache.org/xmlns/features/v1.0.0" name="universAAL-Ontologies">
        <feature name="uAAL-Ont.X73" resolver="(obr)" version="2.0.3-SNAPSHOT">
          <bundle start-level="61" start="true">mvn:org.universAAL.ontology/ont.X73/2.0.3-SNAPSHOT</bundle>
       </feature>
    </features>

A repository of features follows the same structure but contains more than one feature:

    <?xml version="1.0" encoding="UTF-8"?>
    <features xmlns="http://karaf.apache.org/xmlns/features/v1.0.0" name="universAAL-Ontologies">
       <feature name="uAAL-Ont.PhWorld" resolver="(obr)" version="${pom.version}" description="The Physical World Ontolgy.">
          <feature>uAAL-MW</feature>
          <bundle start-level="50">wrap:mvn:jp.go.ipa/jgcl/${jgcl.version}</bundle>
          <bundle start-level="60" start="true">mvn:org.universAAL.ontology/ont.phWorld/${ont.phWorld.version}</bundle>
       </feature>
       <feature name="uAAL-Ont.X73" resolver="(obr)" version="2.0.3-SNAPSHOT">
          <feature>uAAL-Ont.PhWorld</feature>
          <bundle start-level="61" start="true">mvn:org.universAAL.ontology/ont.X73/2.0.3-SNAPSHOT</bundle>
     </feature>
    </features>

Provision schema for bundles can be as follow, being optional options those in {}:

    <bundle {start-level="70”} start='true/false'>{wrap:}mvn:GROUP_ID/ARTIFACT_ID/VERSION</bundle>

Dependent features are useful when a given feature depends on another feature to be installed. Such a dependency can be expressed easily in the feature definition.

   <feature name="uAAL-Ont.PhWorld”>
      <bundle start-level="60" start="true">mvn:org.universAAL.ontology/ont.phWorld/2.0.3-SNAPSHOT</bundle>
   </feature>
   <feature name="uAAL-Ont.Health.Disease">
      <feature>uAAL-Ont.PhWorld</feature>
      <bundle start-level="61" start="true">mvn:org.universAAL.ontology/ont.health.disease/2.0.3-SNAPSHOT</bundle>
   </feature>

The effect of such a dependency is to automatically install the required uAAL-Ont.PhWorld feature when the uAAL-Ont.Health.Disease feature is installed.

References to features define in other repositories are allow and can be achieved by adding a list of repository.

   <?xml version="1.0" encoding="UTF-8"?>
   <features xmlns="http://karaf.apache.org/xmlns/features/v1.0.0" name="universAAL-Security">
          <repository>mvn:org.universAAL.middleware/mw.karaf.feature/2.0.3-SNAPSHOT/xml/features</repository>
          <repository>mvn:org.universAAL.ontology/ont.karaf.feature/2.0.3-SNAPSHOT/xml/features</repository>
          <feature name="uAAL-Security.Authorizator">
             <feature>uAAL-Ont.Profile</feature> <!—Comes from ont.karaf.feature repository-->
             <bundle start="true" start-level="70">mvn:org.universAAL.security/security.authorizator/2.0.3-SNAPSHOT</bundle>
          </feature>
   </features>

By default, all the features defined in a repository are not installed at the launch of Apache Karaf, only those that are referenced at the startup time.

Notice that if we have feature A that is dependent of feature B, and a feature C depends on feature A and B. It isn’t necessary to reference both, only feature B.

Furthermore, this dependencies are only taking into account in runtime.

There is a template you may follow which provides most of the project essentials to build your own feature.

For further information on Features click here.

Be careful when you define them as there is a risk of 'cycling' dependencies.