转自:http://osgi.com.cn/article/7289487
说明:
使用此功能可以多读一下 apache karaf 2.3.3 的使用手册中的 Archetypes
在使用之前请保证你已经下载了 apache karaf 2.3.3 版本并安装在本地
此篇文章使用的环境 mac osx 10.9
准备 Bundle 所依赖的包文件
使用maven命令行创建工具类
#mvn archetype:create -DgroupId=me.laochen.utils -Dversion=1.0 -DartifactId=me-utils-email在me.laoche.utils包中创建Email文件
package me.laochen.utils; public class Email { public String email; public Email(String email) { super(); this.email = email; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public void print() { System.err.println("[me.laochen.utils] print hello world" + this.email); } }
编译并安装该辅助包
#mvn clean install通过warp私有协议安装辅助工具包 (打开apache karaf的命令行 $KARAF_INSTALL_PATH/bin/karaf)
karaf@root>install wrap:mvn:me.laochen.utils/me-utils-email/1.0查看是否安装成功
karaf@root>list|grep -i email
使用maven 命令行创建bundle
# mvn archetype:generate
-DarchetypeGroupId=org.apache.karaf.archetypes
-DarchetypeArtifactId=karaf-bundle-archetype
-DarchetypeVersion=2.3.3
-DgroupId=me.laochen
-DartifactId=me.laochen.user
-Dversion=1.0-SNAPSHOT
-Dpackage=me.laoche.core
为该bundle引入辅助包(修改pom.xml) 添加下面的依赖
me.laochen.utils me-utils-email 1.0
将该项目转化为eclipse
#mvn eclipse:eclipse
添加业务逻辑代码 (修改Activator.java)
package me.laoche.core;import me.laochen.utils.Email;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator { public void start(BundleContext context) { Email email = new Email("3gwind@gmail.com"); email.print(); System.err.println("[start]--hello--"); } public void stop(BundleContext context) { System.out.println("[me.laochen.core]Stopping the bundle"); }}
打包该bundle
#mvn clean package
安装该bundle至karat环境中
karaf@root>install -s file:/pathtobundle/me.laochen.user-1.0-SNAPSHOT.jar
也可以直接复制me.laochen.user-1.0-SNAPSHOT.jar包至$KARAFINSTALLPATH/deploy 目录下
检查是否安装成功
karaf@root>list|grep -i user
如果成功可以看到对应的包是激活状态