Apache Karaf 建立 Bundle

轉自:http://osgi.com.cn/article/7289487

說明:

  • 使用此功能能夠多讀一下 apache karaf 2.3.3 的使用手冊中的 Archetypesjava

  • 在使用以前請保證你已經下載了 apache karaf 2.3.3 版本並安裝在本地apache

  • 此篇文章使用的環境 mac osx 10.9eclipse

準備 Bundle 所依賴的包文件

  • 使用maven命令行建立工具類 
    #mvn archetype:create -DgroupId=me.laochen.utils -Dversion=1.0 -DartifactId=me-utils-emailmaven

  • 在me.laoche.utils包中建立Email文件ide

        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.0this

  • 查看是否安裝成功 
    karaf@root>list|grep -i email 
    spa

使用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.0orm

將該項目轉化爲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 若是成功能夠看到對應的包是激活狀態

相關文章
相關標籤/搜索