前言

OSGi是目前动态模块系统的事实上的工业标准,它适用于任何需要模块化、面向服务、面向组件的应用程序。Eclipse如此庞大和复杂的插件体系,就是基于OSGi。Liferay也是基于OSGi。
OSGi是模块化设计的一种标准,适用于大中型项目。在一些小型项目中,开发者也可以自己设计私有的classLoader机制来实现插件开发环境,比如openfire

OSGi是在Java平台上开发模块化应用程序的一种方式。它允许你构建模块(称之为bundle),它们彼此之间是隔离的,具备明确的和可管理的依赖。
我们的关注点在于依赖,比如说特定的bundle能不能安装到我们的环境之中,是不是还要添加额外的依赖,它对其他模块的影响是什么等等。

OSGi长期以来的工具支持比较糟糕,OSGi本来是轻量级的技术,但因为难以使用而广受诟病,Bndtools是快速开发OSGi bundle和应用的IDE,用于快速的编码/运行/测试。它貌似只支持Eclipse,还不支持其他IDE,IntelliJ有Osmorc 项目,NetBeans会使用Maven Bundle Plugin

Bndtools是由Peter Kriens (OSGi联盟的技术总监)开发的,由Neil Bartlett维护。BndTools希望你可以使用OSGi声明式服务以POJO样式编写组件,而不依赖于OSGi。这样,这些组件就可以在OSGi之外进行单元测试,并在非OSGi的生产环境——像Sprint或JavaEE——中使用。

BndTools的“本地”构建系统是bnd本身,它支持Apache Ant同时构建多个项目,这也是OSGi联盟构建他们的1300多个bundle的方式。Bnd拥有插件化的库系统(pluggable repository system),这让我们可以使用任何一种后端的库技术,包括Ivy。

不贴代码了,直接看下面:

Bndtools OSGi对企业开发带来的好处:
http://www.infoq.com/cn/articles/agile-and-structural-modularity-part3?utm_source=tuicool&utm_medium=referral
案例:西门子公司通过OSGi实现结构性的模块化,由此达到敏捷的目标,与此同时也实现了社交和流程方面的敏捷开发。Bndtools是促成这一解决方案的关键。同时,西门子公司的业务需求也反过来促进和形成了Bndtools的关键功能。

Bndtools安装
http://bndtools.org/installation.html

Bndtools开发入门
http://bndtools.org/tutorial.html

有人说liferay v7像个玩具?那么先来看看它和v6.2的架构区别:

  • liferay v7.0通过微服务改造提取的bundles超过300个;
  • liferay v7.0的扩展集成点超过200,这个在v6.2是不可想象的;

所以继续做v6.2是没有任何意义了。

目前我在不断整理集成点的开发,见:

http://www.cnblogs.com/starcrm/p/6062914.html

如何在Liferay开发中自动把插件构建到osgi容器

这里简单举例说明,先写一个portlet:

注意,@Component是osgi的注解,用于注册组件,组件可以理解为最后颗粒度的类元素。

  • immediate元素控制是否立即激活Component configuration;
  • service元素描述了Component实现的接口,这些接口一定是对外提供服务接口;
  • property元素定义了配置参数
  • 当有依赖项时,还需要有reference元素
package 你的包名;

import java.io.IOException;
import java.io.PrintWriter;
import javax.portlet.GenericPortlet;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse; import org.osgi.service.component.annotations.Component; @Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=YourPortlet",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class YourPortlet extends GenericPortlet { @Override
protected void doView(RenderRequest request, RenderResponse response)
throws IOException, PortletException { PrintWriter printWriter = response.getWriter(); printWriter.print("Hello World!");
} }

Bndtools方式

自动化构建:
只需要运行XXXX.run文件:

-runremote: liferay;shell=-1

-runbundles: \

  你的Porject目录;version=latest,\

-runpath: \
  ../cnf/local/biz.aQute.remote.launcher/biz.aQute.remote.launcher-3.0.0.jar;version=file

注意:biz.aQute.remote.agent-X.X.X.jar这个必须要先存在。
在“你的Porject目录”里必须有这个文件:bnd.bnd
内容:

Bundle-Version: 1.0.0
-sources: true Private-Package: 你的包名.YourPortlet
-buildpath:\
javax.portlet;version='2.0.0',\
org.osgi.service.component.annotations;version='1.3.0'

OSGi原来复杂的构建过程,现在已自动化了。

Maven方式

也可以通过Maven 构建

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>default-bnd-process</id>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.ant.bnd</artifactId>
<version>2.0.28</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

gradle方式

貌似更简单

build.gradle:

dependencies {
compile 'com.liferay.portal:com.liferay.portal.kernel:2.0.0'
compile 'javax.portlet:portlet-api:2.0'
compile 'org.osgi:org.osgi.service.component.annotations:1.3.0'
}


version = '1.0.0'

bnd.bnd 可以大大简略:

Bundle-Version: 1.0.
-sources: true

运行:

../../gradlew deploy

 

更多其他osgi参考资料

osgi大纲(v6.0 2015年版)
https://osgi.org/download/r6/osgi.cmpn-6.0.0.pdf

osgi学习资源:
https://www.osgi.org/developer/resources/learning-resources-tutorials/

http://enroute.osgi.org/book/150-tutorials.html

一个非常好的例子:
http://www.vogella.com/tutorials/OSGi/article.html

Apache Felix OSGi Tutorial:
http://felix.apache.org/documentation/tutorials-examples-and-presentations/apache-felix-osgi-tutorial.html
Neil Bartlett:
http://njbartlett.name/osgibook.html
http://njbartlett.name/files/osgibook_preview_20091217.pdf
Bndtools Tutorial:
http://bndtools.org/tutorial.html

Eclipse Equinox教程
http://www.eclipse.org/equinox/documents/quickstart-framework.php
Beginer Tutorial
http://www.javaworld.com/article/2077837/java-se/java-se-hello-osgi-part-1-bundles-for-beginners.html

Liferay7 BPM门户开发之38: OSGi模块化Bndtools、Maven、Gradle开发构建入门的更多相关文章

  1. Liferay7 BPM门户开发之37: Liferay7下的OSGi Hook集成开发

    hook开发是Liferay客制扩展的一种方式,比插件灵活,即可以扩展liferay门户,也能对原有特性进行更改,Liferay有许多内置的服务,比如用hook甚至可以覆盖Liferay服务. 可作为 ...

  2. Liferay7 BPM门户开发之17: Portlet 生命周期

    Portlet 生命周期 init() =〉 render() =〉 processAction() =〉 processEvent() =〉 serveResource() =〉destroy() ...

  3. Liferay7 BPM门户开发之10: 通用流程实现从Servlet到Portlet(Part1)

    开发目的: 实现通用流程自动化处理(即实现不需要hardcode代码的bpm统一处理后台,仅需要写少量前端html form代码和拖拽设计BPM定义) 既可独立运行或可依托于Liferay或依托其它门 ...

  4. Liferay7 BPM门户开发之12:acitiviti和liferay用户权限体系集成

    写到第12章才出现Liferay的内容,希望可以厚积薄发. 我们的目标是不使用不维护Activiti的用户组织架构,只维护Liferay的体系,这样的好处是非常明显的,即不用做组织架构的同步工作. 原 ...

  5. Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)

    第一步:修改liferay-hook.xml <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Lifer ...

  6. Liferay7 BPM门户开发之46: 集成Activiti用户、用户组、成员关系同步

    在实际的BPM集成开发过程中,Liferay和Activiti这两个异构的系统之间,用户.组的同步需求非常重要,用来实现签收组的概念,比如指定签收组.会签.抢签都需要用到. Activiti可以通过自 ...

  7. Liferay7 BPM门户开发之44: 集成Activiti展示流程列表

    处理依赖关系 集成Activiti之前,必须搞清楚其中的依赖关系,才能在Gradle里进行配置. 依赖关系: 例如,其中activiti-engine依赖于activiti-bpmn-converte ...

  8. Liferay7 BPM门户开发之34: liferay7对外服务类生成(RestService Get Url)

    在liferay7中开发不依赖Service Builder的对外服务类,非常简洁,只需要2点注解: 在类的前部定义: @ApplicationPath("/PathXXX") 方 ...

  9. Liferay7 BPM门户开发之27: MVC Portlet插件工程开发

    官网上的教材说实话实在精简不清晰. https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/creating-an-mvc-por ...

随机推荐

  1. A*算法的原理 <转>

    第一部分:A*算法简介    写这篇文章的初衷是应一个网友的要求,当然我也发现现在有关人工智能的中文站点实在太少,我在这里 抛砖引玉,希望大家都来热心的参与.     还是说正题,我先拿A*算法开刀, ...

  2. 数据库SQL优化大总结之 百万级数据库优化方案

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  3. Notepad++ 默认快捷键

    Notepad++绝对是windows下进行程序编辑的神器之一,要更快速的使用以媲美VIM,必须灵活掌握它的快捷键,下面对notepad++默认的快捷键做个整理(其中有颜色的为常用招数):     1 ...

  4. 使用maven搭建ssh框架

    首先搭建sturts2框架,配置pom文件: <properties> <!-- 文件拷贝时的编码 --> <project.build.sourceEncoding&g ...

  5. Log4j按级别输出到不同文件

    log4j.properties 文件: log4j.logger.net.sf.hibernate.cache=debug log4j.rootLogger = error,portal_log,s ...

  6. mark

    *求数根公式:a的数根b = (a-1) % 9 + 1; *约瑟环问题:f1 = 0; 第i个(i>1),f = (f+m) %i;

  7. Openssl生成证书三板斧

    证书创建三步曲: 一.密钥文件 二.请求文 三.根证书签名 最后看需要是否合并证书文件 1. 创立根证书密钥文件(自己做CA)root.key: [kk@test ~]$ openssl genrsa ...

  8. 基于webdriver的jmeter性能测试-Eclipse+Selenium+JUnit生成jar包

    续接 打开eclipse新建java项目,如下图所示: 输入项目名称后点击"完成"按钮,如下图所示: eclipse中新建一个java项目,如下图所示: 添加类库,如下图所示: 在 ...

  9. 纯css3 Star

    <style><!--* { box-sizing: border-box; padding: 0px; margin: 0px; } body, html { height: 10 ...

  10. Android 开发快速导引:Android程序框架【草】

    概述 学习一项新技术之前要先了解这个技术的整体框架,这里先简单说一下 Android 的程序结构. Android App 有四个顶层的类:Activity.Service.ContentProvid ...