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。2.修改karaf的啓動配置
################################################################################
#
# 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目錄
<?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裏面,可以靈活的安裝和卸載。