发布karaf的features

雷方伟
2023-12-01

Karaf的features是其本身的一大亮点,通过features可以完成某个功能相关bundle的安装和卸载,极大的方便了对bundle的管理。

Karaf的最新版本是2.3.2,通过features:install 命令,安装的features都是在data目录下,这个目录你可以整个的清空,就可以恢复karaf的最初状态。如果我们在开发过程中,打算把你弄好的环境发布出去,该如何完成呢?

带着这个问题,查了karaf的用户手册,发现karaf也提供了相应的支持,不得不感叹老外开发的东西就是全面,只要你有需求,人家都帮你弄好了。

1.安装features插件

<plugin>
                <groupId>org.apache.karaf.tooling</groupId>
                <artifactId>features-maven-plugin</artifactId>
                <version>2.3.2</version>

                <executions>
                    <execution>
                        <id>add-features-to-repo</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>add-features-to-repo</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>file:D:/myJavaTools/apache-karaf-2.3.2/etc/drools.xml</descriptor>
                                <!--<descriptor>mvn:org.apache.activemq/activemq-karaf/5.8.0/xml/features</descriptor>-->
                                <!--<descriptor>mvn:org.apache.karaf.assemblies.features/standard/2.3.2/xml/features</descriptor>-->
                            </descriptors>
                            <features>
                                <feature>war</feature>
                                <feature>webconsole</feature>
                                <feature>drools-common</feature>

                            </features>
                            <repository>${project.build.directory}/features-repo</repository>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
说明:这里我自定义了drools.xml,这个文件时我在drools的官网上下载到本地的,我打算在karaf中预先安装war,webconsole以及drools-common三种features。
执行mvn install后,会在target/features-repo下看到生成的jar包,这些jar是以maven库的格式组织的。

2.修改karaf的启动配置

打开 org.apache.karaf.features.cfg文件,修改如下:
################################################################################
#
#    Licensed to the Apache Software Foundation (ASF) under one or more
#    contributor license agreements.  See the NOTICE file distributed with
#    this work for additional information regarding copyright ownership.
#    The ASF licenses this file to You under the Apache License, Version 2.0
#    (the "License"); you may not use this file except in compliance with
#    the License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
################################################################################

respectStartLvlDuringFeatureStartup=false

#
# Comma separated list of features repositories to register by default
#
featuresRepositories=mvn:org.apache.karaf.assemblies.features/standard/2.3.2/xml/features,mvn:org.apache.karaf.assemblies.features/enterprise/2.3.2/xml/features,file:D:/myJavaTools/apache-karaf-2.3.2/etc/drools.xml

#
# Comma separated list of features to install at startup
#
featuresBoot=config,ssh,management,kar,war,webconsole,drools-common

#
# Defines if the boot features are started in asynchronous mode (in a dedicated thread)
#
featuresBootAsynchronous=false
在featuresRepositories中增加我刚才自定义的drools features文件;然后在featuresBoot增加war,webconsole,drools-common

  3.复制生成的jar到system目录

复制生成的jar包目录下的文件到karaf下的system目录
以上操作完成后,可以尝试清空karaf下的data目录,然后重启karaf,输入 features:list,哈哈,发现war,webconsole以及drools以及安装好了
附件:drools.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<features name="camel-drools-example-6.0.0.CR3"
          xmlns="http://karaf.apache.org/xmlns/features/v1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.0.0 http://karaf.apache.org/xmlns/features/v1.0.0">

  <repository>mvn:org.apache.camel.karaf/apache-camel/2.10.3/xml/features</repository>
  <repository>mvn:org.apache.cxf.karaf/apache-cxf/2.6.6/xml/features</repository>

  <feature name="drools-common" version="6.0.0.CR3" description="Drools Commons">
    <feature version="[2.6,3.0)">cxf-specs</feature>
    <bundle>mvn:com.google.protobuf/protobuf-java/2.5.0</bundle>
    <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/3.5_1</bundle>
    <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.xstream/1.4.3_1</bundle>
    <bundle start-level='10'>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.jaxb-api-2.2/1.9.0</bundle>
    <bundle start-level='10'>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.jaxb-xjc/2.2.1.1_2</bundle>
    <bundle start-level='10'>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.jaxb-impl/2.2.1.1_2</bundle>
    <bundle>mvn:org.mvel/mvel2/2.1.6.Final</bundle>
    <bundle>wrap:mvn:org.eclipse.jdt.core.compiler/ecj/4.2.1$Bundle-SymbolicName=Eclipse-JDT-Compiler&Bundle-Version=4.2.1</bundle>
    <bundle>wrap:mvn:org.codehaus.janino/janino/2.6.1$Bundle-SymbolicName=Codehaus-Janino&Bundle-Version=2.6.1</bundle>
    <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.quartz/2.1.6_1</bundle>
    <bundle>mvn:org.apache.geronimo.specs/geronimo-atinject_1.0_spec/1.0</bundle>
    <bundle>wrap:mvn:javax.enterprise/cdi-api/1.0-SP4</bundle>
  </feature>

  <feature name="drools-module" version="6.0.0.CR3" description="Drools core">
    <feature version="6.0.0.CR3">drools-common</feature>
    <feature version="6.0.0.CR3">kie</feature>
    <bundle>mvn:org.drools/drools-core/6.0.0.CR3</bundle>
    <bundle>mvn:org.drools/drools-compiler/6.0.0.CR3</bundle>
    <bundle>mvn:org.drools/drools-osgi-integration/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools-templates" version="6.0.0.CR3" description="Drools Templates">
    <bundle>mvn:org.drools/drools-templates/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools5-spring" version="6.0.0.CR3" description="Drools5 Spring">
    <feature version="[3.0,4.0)">spring</feature>
    <bundle>mvn:org.drools/drools-spring-legacy5/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools5-camel" version="6.0.0.CR3" description="Drools5 Camel">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="6.0.0.CR3">drools5-spring</feature>
    <feature version="2.10.3">camel</feature>
    <feature version="2.10.3">camel-cxf</feature>
    <feature version="[2.6,3.0)">cxf-specs</feature>
    <feature version="[2.6,3.0)">cxf-core</feature>
    <feature version="[2.6,3.0)">cxf-jaxrs</feature>
    <bundle>mvn:org.drools/drools-camel-legacy5/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools-decisiontable" description="Drools Decision Tables" version="6.0.0.CR3">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="6.0.0.CR3">drools-templates</feature>
    <bundle>mvn:commons-codec/commons-codec/1.7</bundle>
    <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.poi/3.9_2</bundle>
    <bundle>mvn:org.drools/drools-decisiontables/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools-jpa" version="6.0.0.CR3" description="Drools JPA">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="[3.0,4.0)">spring-orm</feature>
    <feature version="[1.0.1,2.0)">jpa</feature>
    <bundle>mvn:org.drools/drools-persistence-jpa/6.0.0.CR3</bundle>
  </feature>

  <feature name="kie" version="6.0.0.CR3">
    <bundle>mvn:org.kie/kie-api/6.0.0.CR3</bundle>
    <bundle>mvn:org.kie/kie-internal/6.0.0.CR3</bundle>
  </feature>

  <feature name="kie-spring" version="6.0.0.CR3">
    <feature version="6.0.0.CR3">kie</feature>
    <feature version="6.0.0.CR3">drools-jpa</feature>
    <feature version="6.0.0.CR3">jbpm</feature>
    <feature version="6.0.0.CR3">jbpm-human-task</feature>
    <bundle>mvn:org.kie/kie-spring/6.0.0.CR3</bundle>
  </feature>

  <feature name="kie-aries-blueprint" version="6.0.0.CR3">
    <feature version="6.0.0.CR3">kie</feature>
    <feature version="6.0.0.CR3">drools-jpa</feature>
    <feature version="6.0.0.CR3">jbpm</feature>
    <feature version="6.0.0.CR3">jbpm-human-task</feature>
    <bundle>mvn:org.kie/kie-aries-blueprint/6.0.0.CR3</bundle>
  </feature>

  <feature name="kie-camel" version="6.0.0.CR3">
    <feature version="6.0.0.CR3">kie</feature>
    <feature version="6.0.0.CR3">kie-spring</feature>
    <feature version="6.0.0.CR3">jbpm</feature>
    <feature version="2.10.3">camel</feature>
    <feature version="2.10.3">camel-cxf</feature>
    <feature version="[2.6,3.0)">cxf-specs</feature>
    <feature version="[2.6,3.0)">cxf-core</feature>
    <feature version="[2.6,3.0)">cxf-jaxrs</feature>
    <bundle>mvn:org.kie/kie-camel/6.0.0.CR3</bundle>
  </feature>

  <feature name="jbpm" version="6.0.0.CR3" description="jBPM engine">
    <feature version="6.0.0.CR3">drools-module</feature>
    <bundle>mvn:joda-time/joda-time/1.6.2</bundle>
    <bundle>mvn:org.jbpm/jbpm-flow-builder/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-flow/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-bpmn2/6.0.0.CR3</bundle>
  </feature>

  <feature name="jbpm-human-task" version="6.0.0.CR3" description="jBPM engine">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="6.0.0.CR3">drools-jpa</feature>
    <feature version="6.0.0.CR3">jbpm</feature>
    <bundle>mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
    <bundle>mvn:org.apache.geronimo.specs/geronimo-jpa_2.0_spec/1.1</bundle>
    <bundle>mvn:org.apache.geronimo.specs/geronimo-jms_1.1_spec/1.1.1</bundle>
    <bundle>wrap:mvn:org.apache.geronimo.specs/geronimo-jacc_1.1_spec/1.0</bundle>
    <bundle>wrap:mvn:org.jboss.seam.transaction/seam-transaction-api/3.1.0.Final</bundle>
    <bundle>wrap:mvn:org.jboss.weld/weld-core/1.1.13.Final</bundle>
    <bundle>wrap:mvn:org.jboss.solder/solder-api/3.2.0.Final</bundle>
    <bundle>wrap:mvn:org.apache.helix/helix-core/0.6.1-incubating</bundle>
    <bundle>mvn:org.kie.commons/kie-nio2-model/6.0.0.CR3</bundle>
    <bundle>mvn:org.kie.commons/kie-commons-cdi/6.0.0.CR3</bundle>
    <bundle>mvn:org.kie.commons/kie-commons-validation/6.0.0.CR3</bundle>
    <bundle>mvn:org.kie.commons/kie-commons-data/6.0.0.CR3</bundle>
    <bundle>mvn:org.kie.commons/kie-commons-io/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-audit/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-shared-services/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-runtime-manager/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-human-task-core/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-human-task-workitems/6.0.0.CR3</bundle>
    <bundle>mvn:org.jbpm/jbpm-persistence-jpa/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools5-camel-example" version="6.0.0.CR3" description="Drools5 Spring Camel Example">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="6.0.0.CR3">drools-decisiontable</feature>
    <feature version="6.0.0.CR3">drools5-spring</feature>
    <feature version="6.0.0.CR3">drools5-camel</feature>
    <bundle>mvn:org.drools/drools-camel-example/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools-camel-example" version="6.0.0.CR3" description="Drools Kie Spring Camel Example">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="6.0.0.CR3">drools-decisiontable</feature>
    <feature version="6.0.0.CR3">kie-spring</feature>
    <feature version="6.0.0.CR3">kie-camel</feature>
    <bundle start-level="100">mvn:org.drools/spring-camel-drools-decision-table-example/6.0.0.CR3</bundle>
  </feature>

  <feature name="drools-blueprint-camel-example" version="6.0.0.CR3" description="Drools Kie Blueprint Camel Example">
    <feature version="6.0.0.CR3">drools-module</feature>
    <feature version="6.0.0.CR3">drools-decisiontable</feature>
    <feature version="6.0.0.CR3">kie-aries-blueprint</feature>
    <feature version="6.0.0.CR3">kie-camel</feature>
    <bundle>mvn:org.drools/blueprint-camel-drools-decision-table-example/6.0.0.CR3</bundle>
  </feature>

  <feature name="jbpm-example" version="6.0.0.CR3" description="jBPM Spring Example">
    <feature version="6.0.0.CR3">jbpm</feature>
    <bundle>mvn:org.drools/jbpm-osgi-example/6.0.0.CR3</bundle>
  </feature>

</features>

总结:以上的过程,对于大家来说,有什么启示吗?我想大家肯定会想到,在使用karaf开发osgi的项目过程中,把相关的功能通过features进行组织,然后按照上面的方式,把预先必须的features预先设置,然后进行发布,系统提供的其他features也包括在你发布的karaf里面,可以灵活的安装和卸载。
 类似资料: