maven,OSGI,karaf 学习笔记

研一时在 opendaylight 控制器上开发过路由插件,当时开发时半懂不懂的,很多东西都没弄清楚。最近为找找工作做准备,准备重新好好学习 opendatlight。
opendatlight 源码中涉及到的知识有:maven,OSGI,karaf,yang 技术;Config Subsystem,MD-SAL,MD-SAL Clustering 子系统;NETCONF,RESTCONF 协议。

maven

At first glance Maven can appear to be many things, but in a nutshell Maven is an attempt to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices. Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Distribution
  • Releases

maven 是项目管理工具,确实很好用。自己接触过的使用 maven 管理的项目:opendaylight,spring,hibernate。

pom.xml 文件标签详解:https://maven.apache.org/pom.html
配置参考网址:https://maven.apache.org/settings.html

可以打开一个项目的 pom.xml 查看,遇到不认得的标签就去上面网页查,这样学习比较快。
我觉得理解了 pom.xml 文件标签含义和 maven 工具的配置,就算 maven 入门了。

顺便附一个官网入门指南:https://maven.apache.org/guides/getting-started/index.html
因为自己原来已经接触过 maven 了,对自己用处不太大。

maven 插件

深入学习的话,需要学习 maven 强大的插件功能。
https://maven.apache.org/plugins/index.html

我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven-compiler-plugin完成的。进一步说,每个任务对应了一个插件目标(goal),每个插件会有一个或者多个目标,例如maven-compiler-plugin的compile目标用来编译位于src/main/java/目录下的主源码,testCompile目标用来编译位于src/test/java/目录下的测试源码。

用户可以通过两种方式调用Maven插件目标。第一种方式是将插件目标与生命周期阶段(lifecycle phase)绑定,这样用户在命令行只是输入生命周期阶段而已,例如Maven默认将maven-compiler-plugin的compile目标与compile生命周期阶段绑定,因此命令mvn compile实际上是先定位到compile这一生命周期阶段,然后再根据绑定关系调用maven-compiler-plugin的compile目标。第二种方式是直接在命令行指定要执行的插件目标,例如mvn archetype:generate 就表示调用maven-archetype-plugin的generate目标,这种带冒号的调用方式与生命周期无关。

maven plugin 案例分析

在 opendaylight 的各个子项目中都引入了 maven-bundle-plugin,下面列出 routing 中 pom.xml 部分内容。

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Import-Package>org.slf4j,
              org.opendaylight.controller.sal.routing,
              org.opendaylight.controller.sal.core,
              org.opendaylight.controller.sal.topology,
              org.opendaylight.controller.sal.utils,
              org.opendaylight.controller.sal.reader,
              org.opendaylight.controller.clustering.services,
              org.apache.commons.collections15,
              org.opendaylight.controller.switchmanager,
              org.opendaylight.controller.topologymanager,
              edu.uci.ics.jung.graph,
              edu.uci.ics.jung.algorithms.shortestpath,
              edu.uci.ics.jung.graph.util,
              org.apache.felix.dm,
              org.osgi.framework,
              org.apache.felix.service.command,
              org.junit;resolution:=optional</Import-Package>
            <Bundle-Activator>org.opendaylight.controller.routing.dijkstra_implementation.internal.Activator</Bundle-Activator>
          </instructions>
          <manifestLocation>${project.basedir}/META-INF</manifestLocation>
        </configuration>
      </plugin>
    </plugins>
  </build>

网上查看 maven-bundle-plugin 的执行目标信息如下:

General Information about the goals.

bundle:bundle Create an OSGi bundle from Maven project. (life-cycle goal)
bundle:manifest Generate an OSGi manifest for this project.
bundle:cleanVersions Convert a group of versions to OSGi format.
bundle:instructions Generate BND instructions for this project.
bundle:install Installs bundle details in the local OBR repository. (life-cycle goal)
bundle:deploy Deploys bundle details to a remote OBR repository. (life-cycle goal)
bundle:index Index the content of a maven repository using OBR.
bundle:install-file Installs bundle details in the local OBR repository. (command-line goal)
bundle:deploy-file Deploys bundle details to a remote OBR repository. (command-line goal)
bundle:clean Clean a local OBR repository by finding and removing missing resources.
bundle:remote-clean Clean a remote OBR repository by finding and removing missing resources.
bundle:ant Generate Ant script to create the bundle. (you should run ant:ant first)

可以看出这个插件中部分 goals 是与生命周期绑定的,部分没有。我们执行 maven:install 其实调用的 bundle:install 功能。

再附上几个我原来看过的中文网址:

  1. Apache Maven 入门篇 ( 上 )
  2. Apache Maven 入门篇 ( 下 )
  3. 细致全面的Maven教程
  4. maven 插件介绍
  5. maven-bundle-plugin

待解决疑惑:

opendaylight 提供的 settings.xml 文件中,有两个 activeProfile 标签,看网上的解释说可以激活多个 profile,那都激活后哪一个会起作用?

<activeProfiles>
    <activeProfile>opendaylight-release</activeProfile>
    <activeProfile>opendaylight-snapshots</activeProfile>
  </activeProfiles>

参考网址:http://blog.csdn.net/taiyangdao/article/details/52311257

OSGI 入门

OSGI 是一个标准,实现这个标准的产品有:apache felix,Equinox等。

OSGI 架构图

OSGI 可以分为三层:模块层,生命周期层,服务层。
每一层的学习可以看下面的入门教程。

参考书目和文档:

  1. 《深入理解OSGi Equinox原理、应用与最佳实践》,讲的很详细。
  2. 官网文档
  3. 中文社区
  4. 入门教程
  5. OSGi Core Release 6 Specification
  6. 里面有 Specifications,Javadoc,XML Schemas 等内容

karaf

Karaf Container is a modern and polymorphic container.It’s a lightweight, powerful, and enterprise ready container powered by OSGi.

karaf 架构图

Karaf安装目录结构如下:

/bin: 启动脚本
/etc: 初始化文件
/data: 工作目录
/cache: OSGi框架包缓存
/generated-bundles: 部署使用的临时文件夹
/log: 日志文件
/deploy: 热部署目录
/instances: 含有子实例的目录
/lib: 包含引导库
/lib/ext:JRE扩展目录
/lib/endorsed: 赞同库目录
/system: OSGi包库,作为一个Maven2存储库

Data文件夹包括karaf所有的工作和临时文件,如果你想从一个初始状态重启,你可以清空这个目录,这和“恢复初始化设置”一样的效果。
摘自: http://www.osgi.com.cn/article/7289403

karaf 使用教程:http://karaf.apache.org/manual/latest/
上面教程学习了一部分,下次有需要再看。

待解决疑惑:
felix 是如何确定 bundle 的启动级别的?
karaf 呢?它又是如何确定 bundle 的启动级别的?

第一个问题,我觉得跟 OSGI 的依赖解析规则有关,根据解析的结果,确定启动顺序。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值