基于Maven构建OSGI(二)

迁移自个人百度空间博客,翻译自Nexus和maven相关文档!

 

6、部署OSGI Bundle 到Maven 仓库

 

首先下载Nexus professional(收费),地址为:

http://nexus.sonatype.org/download-nexus.html

当前最新版本为1.9.2.2。

压缩文件为:

 



解压之后有两个文件夹,如下图:

 



进入 nexus-professional-webapp-1.9.2.2\bin\jsw 目录找到和自己相应系统的文件夹,如下图所示:

 



我的为 windows-x86-32 。然后执行 nexus.bat 。如下图:

 



 
 

在地址栏输入: http://localhost:8081/nexus/

 

如图:

 



 
 

点击Log In。admin用户的密码为admin123. d eployment 用户的密码为 deployment 123。

 

设置~/.m2/setting.xml文件的内容如下:

 

<settings>

  <mirrors>

    <mirror>

      <!--This sends everything else to /public -->

      <id>nexus</id>

      <mirrorOf>*</mirrorOf>

      <url>http://localhost:8081/nexus/content/groups/public</url>

    </mirror>

  </mirrors>

  <profiles>

    <profile>

      <id>nexus</id>

      <!--Enable snapshots for the built in central repo to direct -->

      <!--all requests to nexus via the mirror -->

      <repositories>

        <repository>

          <id>central</id>

          <url>http://central</url>

          <releases><enabled>true</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </repository>

      </repositories>

     <pluginRepositories>

        <pluginRepository>

          <id>central</id>

          <url>http://central</url>

          <releases><enabled>true</enabled></releases>

          <snapshots><enabled>true</enabled></snapshots>

        </pluginRepository>

      </pluginRepositories>

    </profile>

  </profiles>

  <servers>

    <server>

      <id>nx-snapshots</id>

      <username>deployment</username>

      <password>deployment123</password>

    </server>

    <server>

      <id>nx-releases</id>

      <username>deployment</username>

      <password>deployment123</password>

    </server>

  </servers>

  <activeProfiles>

    <!--make the profile active all the time -->

    <activeProfile>nexus</activeProfile>

  </activeProfiles>

</settings>

 

 

设置父级pom.xml文件,在<project>节点下加入如下内容:

 

  <distributionManagement>

   <repository>

   <id> nx -releases </id>

   <name> Nexus Releases </name>

   <url>http://localhost:8081/nexus/content/repositories/releases </url>

    </repository>

    <snapshotRepository>

      <id> nx -snapshots </id>

      <name> Nexus Snapshots </name>

 <url> http://localhost:8081/nexus/content/repositories/snapshots </url>

    </snapshotRepository>

  </distributionManagement>

 

新建 Maven 构建命令, deploy – DremoteOBR 。然后会在 Snapshots 中看到如下图所示的内容:

 



 
 

完成此步骤之后,就可以在Maven中引用其他文件一样来依赖该jar文件。例如:

<dependency>

  <groupId> com.yihua.osgi.test- osgi </groupId>

  <artifactId>IQueryWor </artifactId>

  <version> 0.0.1-SNAPSHOT </version>

 </dependency>

 

 

 

注册web应用请参考如下实例程序:

 

package testweb.internal;

 

import javax.servlet.Servlet;

 

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.ServiceEvent;

import org.osgi.framework.ServiceListener;

import org.osgi.framework.ServiceReference;

import org.osgi.service.http.HttpService;

 

 

/**

* Extension of the default OSGi bundle activator

*/

public final class ExampleActivator implements BundleActivator, ServiceListener {

              private BundleContext bundleContext;

 

              private ServiceReference ref;

 

              private Servlet servlet;

 

              public void start(BundleContext context) throws Exception {

                            bundleContext = context;

                            servlet = new QueryWebServlet(bundleContext);

 

                            // 在HttpService中注册Servlet

                            registerServlet();

              }

 

              public void stop(BundleContext context) throws Exception {

                            try {

                                          unregisterServlet();

                            } catch (Throwable t) {

                                          t.printStackTrace();

                            }

 

                            servlet = null;

                            bundleContext = null;

                            ref = null;

              }

 

              public void serviceChanged(ServiceEvent event) {

                            switch (event.getType()) {

                            case ServiceEvent.REGISTERED:

                                          registerServlet();

                                          break;

 

                            case ServiceEvent.UNREGISTERING:

                                          unregisterServlet();

                                          break;

                            }

              }

 

              /*

              * 注册Web应用

              */

              private void registerServlet() {

                            if (ref == null) {

                                          ref = bundleContext

                                                                      .getServiceReference(HttpService.class.getName());

                            }

 

                            if (ref != null) {

                                          try {

                                                        HttpService http = (HttpService) bundleContext.getService(ref);

                                                        if (null != http) {

                                                                      // 注册动态资源Servlet

                                                                      http.registerServlet("/osgi/servlet", servlet, null, null);

                                                                      // 注册静态资源HTML

                                                                      http.registerResources("/osgi/file", "webapp", null);

                                                        }

                                          } catch (Exception e) {

                                                        e.printStackTrace();

                                          }

                            }

              }

 

              /*

              * 卸载Web应用

              */

              private void unregisterServlet() {

                            if (ref != null) {

                                          try {

                                                        HttpService http = (HttpService) bundleContext.getService(ref);

                                                        if (null != http) {

                                                                      http.unregister("/osgi/servlet");

                                                                      http.unregister("/osgi/file");

                                                        }

                                          } catch (Exception e) {

                                                        e.printStackTrace();

                                          }

                            }

              }

}

 

更详尽的内容请参考其他相关OSGI资料。

 

转载请注明文章出处!!!!!!

 

 

迁移自个人百度空间博客,翻译自Nexus和maven相关文档!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值