基于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 目录找到和自己相应系统的文件夹,如下图所示:

 


基于Maven构建OSGI(二)_第1张图片


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

 


基于Maven构建OSGI(二)_第2张图片
 
 

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

 

如图:

 


基于Maven构建OSGI(二)_第3张图片
 
 

点击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构建OSGI(二)_第4张图片
 
 

完成此步骤之后,就可以在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相关文档!

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