Apache Karaf自定义feature

系统环境

JDK :1.8.0_66 x64

Karaf:apache-karaf-4.0.2

maven:Apache Maven 3.3.3


karaf中的feature通常是几个bundle的集合,安装这个feature的时候,相应的bundle也都会被安装上去,用来管理bundle很方便

下面来介绍如何自己开发一个feature

这里来制作一个名字为:apache-commons-utils的feature,里面包含如下bundle

<bundle>mvn:commons-io/commons-io/2.4</bundle>
<bundle>mvn:commons-lang/commons-lang/2.6</bundle>
<bundle>mvn:commons-discovery/commons-discovery/0.5</bundle>
<bundle>mvn:commons-math/commons-math/1.2</bundle>
<bundle>mvn:commons-net/commons-net/3.3</bundle>
再加上2个配置文件 http.cfg、jdbc.cfg (配置文件只是为了演示如何在安装feature的时候,也同时把配置文件安装上)

新建一个maven项目,groupId:com.lala,artifactId:demo-features,version:1.0.0

1:resources目录下,新建http.cfg、jdbc.cfg两个配置文件,内容随便

2:resources目录下,新建features.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<features name="karaf-cellar-4.0.0" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0" 
		  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		  xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.3.0 http://karaf.apache.org/xmlns/features/v1.3.0">

	<feature name="apache-commons-utils" description="apache commons utils" version="1.0.0">
        <!-- 这个配置文件将会安装到etc/目录下 -->
        <configfile finalname="/etc/demo-features.jdbc.cfg">mvn:com.lala/demo-features/1.0.0/cfg/jdbc</configfile>
        <!-- 这个配置文件将会安装到deploy/目录下 -->
        <configfile finalname="/deploy/demo-features.http.cfg" override="true">mvn:com.lala/demo-features/1.0.0/cfg/http</configfile>
        <bundle>mvn:commons-io/commons-io/2.4</bundle>
        <bundle>mvn:commons-lang/commons-lang/2.6</bundle>
        <bundle>mvn:commons-discovery/commons-discovery/0.5</bundle>
        <bundle>mvn:commons-math/commons-math/1.2</bundle>
        <bundle>mvn:commons-net/commons-net/3.3</bundle>
    </feature>

</features>

3:在pom.xml加上如下插件配置

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<configuration>
		<useDefaultDelimiters>false</useDefaultDelimiters>
		<delimiters>
			<delimiter>${*}</delimiter>
		</delimiters>
	</configuration>
	<executions>
		<execution>
			<id>filter</id>
			<phase>generate-resources</phase>
			<goals>
				<goal>resources</goal>
			</goals>
		</execution>
	</executions>
</plugin>
<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<version>1.9.1</version>
	<executions>
		<execution>
			<id>attach-artifact</id>
			<phase>package</phase>
			<goals>
				<goal>attach-artifact</goal>
			</goals>
			<configuration>
				<artifacts>
					<artifact>
						<file>target/classes/features.xml</file>
						<type>xml</type>
						<classifier>features</classifier>
					</artifact>
					<artifact>
						<file>target/classes/jdbc.cfg</file>
						<type>cfg</type>
						<classifier>jdbc</classifier>
					</artifact>
					<artifact>
						<file>target/classes/http.cfg</file>
						<type>cfg</type>
						<classifier>http</classifier>
					</artifact>
				</artifacts>
			</configuration>
		</execution>
	</executions>
</plugin>

注意:这里的artifact、file、type、classifier的配置方法为:

type为file的扩展名,classifier为file的文件名


配置好之后,执行mvn clean install ,mvn clean deploy


4:修改${karaf.home}/etc/org.apache.karaf.features.cfg文件

在featuresRepositories配置项后面加上:mvn:com.lala/demo-features/1.0.0/xml/features


5:修改${karaf.home}/etc/org.ops4j.pax.url.mvn.cfg文件,

在org.ops4j.pax.url.mvn.repositories配置项后面加上:http://192.168.1.100:8080/nexus/content/groups/public  (maven私服的地址)


org.ops4j.pax.url.mvn.settings

org.ops4j.pax.url.mvn.localRepository

这2个配置项,如果你maven本地的仓库地址变了,就需要修改,否则,就不需要修改


5:修改好了之后,重启karaf,执行安装,即可看到效果

karaf@root()> feature:install apache-commons-utils
karaf@root()> list
START LEVEL 100 , List Threshold: 50
ID | State  | Lvl | Version | Name
--------------------------------------------------------
52 | Active |  80 | 0.5     | Commons Discovery
53 | Active |  80 | 2.4.0   | Commons IO
54 | Active |  80 | 2.6     | Commons Lang
55 | Active |  80 | 1.2     | Apache Commons Math Bundle
56 | Active |  80 | 3.3.0   | Commons Net
karaf@root()>



你可能感兴趣的:(osgi,karaf,servicemix)