基于Karaf的项目离线打包与发布

Karaf对OSGi Bundle的组织采用Pax的maven url方式,这也隐性的继承了maven的特性,即,所有的依赖来自maven仓库,在实际开发中,这大大的便利了项目结构的组织和维护,开发人员只要指明对某个bundle的maven依赖,karaf自动去选取和下载。但是,在基于karaf的项目部署时,项目或产品的发布环境往往要求严格,不能够随意访问远程的maven仓库;那提供一个本地maven仓库?显然这也不合理,本地maven仓库的维护加重了维护的复杂性。本文主要就离线发布karaf的需求实现进行探讨。

Pax URL Mvn协议

在讨论karaf的maven依赖加载与管理之前,我们先了解一下Pax URL项目的Mvn协议:

mvn-uri := 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ]
repository-url := < rfc2396 uri > ; an url that points to a maven 2 repository
group-id := < maven groupId > ; group id of maven artifact
artifact-id := < maven artifactId > ; artifact id of maven artifact
version := < maven version > | 'LATEST' | range ; version of maven artifact
range := ( '[' | '(' ) version ',' version ( ')' | ']' )
type := < maven type> ; type of maven artifact
classifier := < maven classifier> ; maven artifact classifier

假设项目需要依赖Pax Web的service-0.2.0-SNAPSHOT.jar,对应的Mvn协议示例如下:

mvn:org.ops4j.pax.web.bundles/service
mvn:org.ops4j.pax.web.bundles/service/LATEST
mvn:org.ops4j.pax.web.bundles/service/0.2.0-SNAPSHOT
mvn:http://repository.ops4j.org/maven2!org.ops4j.pax.web.bundles/service/0.2.0

mvn:file://c:/localRepo!org.ops4j.pax.web.bundles/service/0.2.0 - 从目录中加载

mvn:org.ops4j.pax.web/pax-web-features/3.0.4-SNAPSHOT/xml/features --标识pax-web-features-3.0.4-SNAPSHOT-features.xml构件

协议要点

1)如果版本(version)未指定,将使用默认值“LATEST”,并从可用的Maven元数据(metadata)中解析最新的版本,maven metadata的示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0"><groupId>org.ops4j.pax.web</groupId><artifactId>pax-web-features</artifactId><version>3.0.4-SNAPSHOT</version><versioning><snapshot><timestamp>20131113.170334</timestamp><buildNumber>2</buildNumber></snapshot><lastUpdated>20131113170334</lastUpdated><snapshotVersions><snapshotVersion><extension>pom</extension><value>3.0.4-20131113.170334-2</value><updated>20131113170334</updated></snapshotVersion><snapshotVersion><classifier>features</classifier><extension>xml</extension><value>3.0.4-20131113.170334-2</value><updated>20131113170334</updated></snapshotVersion><snapshotVersion><classifier>jettyconfig</classifier><extension>xml</extension><value>3.0.4-20131113.170334-2</value><updated>20131113170334</updated></snapshotVersion></snapshotVersions></versioning>
</metadata>

2)如果version是SNAPSHOT版本,则采用与maven一致的策略来解析SNAPSHOT版本;

3)如果type未指定,默认值为JAR;

4)如果指定了一个type但没有指定version,则必须为version留有占位即:mvn:groups/artifact//type

5)Pax URL从0.3.0版本开始支持类别Classifier,如果类别classifier未指定,则不启用类别;

6)如果指定了类别,但没有指定版本version或类型type,则必须未版本和(或)类型留有占位,即:

mvn:groups/artifact///classifier

mvn:groups/artifact/version//classifier

mvn:groups/artifact//type/classifier

版本范围

从0.2.0版本开始,Pax URL Mvn支持版本范围,示例如下

  • [1.2.3, 4.5.6) — 1.2.3 <= x < 4.5.6
  • [1.2.3, 4.5.6] — 1.2.3 <= x <= 4.5.6
  • (1.2.3, 4.5.6) — 1.2.3 < x < 4.5.6
  • (1.2.3, 4.5.6] — 1.2.3 < x <= 4.5.6

Mvn协议的参数配置

可以通过两种方式来配置Mvn协议:1)通过OSGi CM;2)通过框架/系统变量。通过CM配置的参数具有高优先级。

参数名称

描述

org.ops4j.pax.url.mvn.certificateCheck

optional; true/false if the SSL certificate check should be done. Default false.

org.ops4j.pax.url.mvn.globalUpdatePolicy

optional; 控制远程maven仓库的更新策略,maven通过本地POM的时间戳比对为依据。可选的策略包括:

always, daily (default), interval:X (where X is an integer in minutes) or never.

org.ops4j.pax.url.mvn.localRepository

optional; 指向本地仓库目录的路径

org.ops4j.pax.url.mvn.repositories

optional; 以逗号分隔的仓库URL列表

org.ops4j.pax.url.mvn.defaultRepositories

optional; 以逗号分隔的默认仓库URL列表

org.ops4j.pax.url.mvn.settings

optional; 指向maven settings.xml的路径

org.ops4j.pax.url.mvn.proxySupport

optional; true/false if the proxy support is enabled . Default true.

Settings.xml

如果有必要且maven settings.xml可用,Pax URL Mvn按如下步骤查找settings.xml:

  1. 查找配置参数
  2. 如果未找到,查找 ${user.home}/.m2/settings.xml
  3. 如果未找到,查找 ${maven.home}/conf/settings.xml
  4. 如果未找到,查找 ${M2_HOME}/conf/settings.xml

Local repository

Maven本地仓库的查找顺序如下:

  1. 查找配置参数定义;
  2. 如果未找到,在settings.xml中进行查找
  3. 如果未找到,默认认为仓库位于${user.home}/.m2/repository.

Repositories

Maven仓库的解析顺序如下:

  1. 在配置参数中查找
  2. 在settings.xml中查找

如果配置参数中org.ops4j.pax.url.mvn.repositories的值以'+'开始,则来自settings.xml中的仓库地址也将附加到此配置的仓库列表中。

设定仓库包含release/snapshot

可以在参数配置的仓库URL中通过添加如下标识来指定仓库所含有的构件类型(不区分大小写):

  • 启用snapshots — 添加 @snapshots
  • 禁用releases — 添加 @noreleases

示例如下:

http://repository.ops4j.org ; 默认不包含任何标识,只查找releases, 不包括snapshots
http://repository.ops4j.org/mvn-snapshots@snapshots ; 同时在此仓库查找releases和snapshots
http://repository.ops4j.org/mvn-snapshots@snapshots@noreleases ; 仅查找snapshots,
不查找releases

默认仓库

在某些情况下,通过默认仓库配置,可以防止用户配置对下载构件造成影响。如果对org.ops4j.pax.url.mvn.defaultRepositories进行了设置,则Pax URL Mvn首先检查此参数设定的默认仓库列表,如果未找到,则继续查找本地仓库和其它在org.ops4j.pax.url.mvn.repositories中配置的仓库。

Karaf中Maven仓库处理策略的配置

Karaf通过CM(config admin)服务对Pax URL Mvn协议处理进行参数配置,具体的配置是位于${karaf.home}/etc下的

org.ops4j.pax.url.mvn.cfg文件:

#
# If set to true, the following property will not allow any certificate to be used
# when accessing Maven repositories through SSL
#
#org.ops4j.pax.url.mvn.certificateCheck=#
# Path to the local Maven settings file.
# The repositories defined in this file will be automatically added to the list
# of default repositories if the 'org.ops4j.pax.url.mvn.repositories' property
# below is not set.
# The following locations are checked for the existence of the settings.xml file
#   * 1. looks for the specified url
#   * 2. if not found looks for ${user.home}/.m2/settings.xml
#   * 3. if not found looks for ${maven.home}/conf/settings.xml
#   * 4. if not found looks for ${M2_HOME}/conf/settings.xml
#
#org.ops4j.pax.url.mvn.settings=#
# Path to the local Maven repository which is used to avoid downloading
# artifacts when they already exist locally.
# The value of this property will be extracted from the settings.xml file
# above, or defaulted to:
#     System.getProperty( "user.home" ) + "/.m2/repository"
#
#org.ops4j.pax.url.mvn.localRepository=#
# Default this to false. It's just weird to use undocumented repos
#
org.ops4j.pax.url.mvn.useFallbackRepositories=false#
# Uncomment if you don't wanna use the proxy settings
# from the Maven conf/settings.xml file
#
# org.ops4j.pax.url.mvn.proxySupport=false#
# Disable aether support by default.  This ensure that the defaultRepositories
# below will be used
#
#org.ops4j.pax.url.mvn.disableAether=true#
# Comma separated list of repositories scanned when resolving an artifact.
# Those repositories will be checked before iterating through the
#    below list of repositories and even before the local repository
# A repository url can be appended with zero or more of the following flags:
#    @snapshots  : the repository contains snaphots
#    @noreleases : the repository does not contain any released artifacts
#
# The following property value will add the system folder as a repo.
#
#org.ops4j.pax.url.mvn.defaultRepositories=# Use the default local repo (e.g.~/.m2/repository) as a "remote" repo
org.ops4j.pax.url.mvn.defaultLocalRepoAsRemote=false#
# Comma separated list of repositories scanned when resolving an artifact.
# The default list includes the following repositories containing releases:
#    http://repo1.maven.org/maven2
#    http://repository.apache.org/content/groups/snapshots-group
#    http://svn.apache.org/repos/asf/servicemix/m2-repo
#    http://repository.springsource.com/maven/bundles/release
#    http://repository.springsource.com/maven/bundles/external
# To add repositories to the default ones, prepend '+' to the list of repositories
# to add.
# A repository url can be appended with zero or more of the following flags:
#    @snapshots  : the repository contains snaphots
#    @noreleases : the repository does not contain any released artifacts
#    @id=reponid : the id for the repository, just like in the settings.xml this is optional but recomendet
#
# The default list doesn't contain any repository containing snapshots as it can impact the artifacts resolution.
# You may want to add the following repositories containing snapshots:
#    http://repository.apache.org/content/groups/snapshots-group@id=apache@snapshots@noreleases
#    http://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@norelease
#    http://oss.sonatype.org/content/repositories/ops4j-snapshots@id=ops4j.sonatype.snapshots.deploy@snapshots@noreleases
#
org.ops4j.pax.url.mvn.repositories= \http://repo1.maven.org/maven2@id=central, \http://repository.springsource.com/maven/bundles/release@id=spring.ebr, \file:${karaf.home}/${karaf.default.repository}@id=system.repository, \file:${karaf.data}/kar@id=kar.repository@multi

下面我们来看一下如何通过调整此配置文件,使得Karaf不再依赖远程Maven仓库和本地Maven仓库,让Karaf发布包随意离线部署成为现实。

首先我们来看一下在org.ops4j.pax.url.mvn.repositories配置中存在两个指向目录路径的仓库URL,即:

file:${karaf.home}/${karaf.default.repository}@id=system.repository, 和 file:${karaf.data}/kar@id=kar.repository@multi。file:${karaf.home}/${karaf.default.repository}@id=system.repository 实际指向的是karaf安装目录下的system目录(也可以根据需要自行调整,karaf.default.repository参数位于etc/system.properties文件中);file:${karaf.data}/kar@id=kar.repository@multi实际指向的是karaf安装目录下的data/kar目录。

通过禁用其他仓库URL,只启用这两个位置仓库,则karaf发布程序即可实现离线部署。

具体配置调整如下:

# Path to the local Maven settings file.
# The repositories defined in this file will be automatically added to the list
# of default repositories if the 'org.ops4j.pax.url.mvn.repositories' property
# below is not set.
# The following locations are checked for the existence of the settings.xml file
#   * 1. looks for the specified url
#   * 2. if not found looks for ${user.home}/.m2/settings.xml
#   * 3. if not found looks for ${maven.home}/conf/settings.xml
#   * 4. if not found looks for ${M2_HOME}/conf/settings.xml
#
org.ops4j.pax.url.mvn.settings=#
# Path to the local Maven repository which is used to avoid downloading
# artifacts when they already exist locally.
# The value of this property will be extracted from the settings.xml file
# above, or defaulted to:
#     System.getProperty( "user.home" ) + "/.m2/repository"
#
org.ops4j.pax.url.mvn.localRepository=file:${karaf.home}/${karaf.default.repository}@id=system.repository@snapshots# Use the default local repo (e.g.~/.m2/repository) as a "remote" repo
org.ops4j.pax.url.mvn.defaultLocalRepoAsRemote=true#
# Comma separated list of repositories scanned when resolving an artifact.
# The default list includes the following repositories:
#    http://repo1.maven.org/maven2@id=central
#    http://repository.apache.org/content/groups/snapshots-group@id=apache@snapshots@noreleases
#    http://repository.springsource.com/maven/bundles/release@id=spring.ebr
#    http://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@norelease
#    http://oss.sonatype.org/content/repositories/ops4j-snapshots@id=ops4j.sonatype.snapshots.deploy@snapshots@noreleases
#    file:${karaf.home}/${karaf.default.repository}@id=system.repository
#    file:${karaf.data}/kar@id=kar.repository@multi
# To add repositories to the default ones, prepend '+' to the list of repositories
# to add.
# A repository url can be appended with zero or more of the following flags:
#    @snapshots  : the repository contains snapshots
#    @noreleases : the repository does not contain any released artifacts
#    @id=repository.id : the id for the repository, just like in the settings.xml this is optional but recommended
#
org.ops4j.pax.url.mvn.repositories= \file:${karaf.home}/${karaf.default.repository}@id=system.repository@snapshots, \file:${karaf.data}/kar@id=kar.repository@multi

在此模式下,Pax URL Mvn优先检查system仓库,如果不存在,则检查${user.home}/.m2下的配置,如果没有settings.xml和repository目录,则加载依赖失败。当system库下不存在org.apache.servicemix.specs.activation-api-1.1-1.9.0.jar时,运行日志如下:

46 -
org.ops4j.pax.url.mvn - 1.6.0 | Resolving
[mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.activation-api-1.1/1.9.0]
46 -
org.ops4j.pax.url.mvn - 1.6.0 | Using manager SimpleLocalRepositoryManager with
priority 0 for D:\Projects\karaf\assemblies\apache-karaf\target\assembly\system
46 - org.ops4j.pax.url.mvn - 1.6.0 | Using connector
WagonRepositoryConnector with priority 0 for file:/C:/Users/xxxx/.m2/repository/
46 -
org.ops4j.pax.url.mvn - 1.6.0 | Using connector WagonRepositoryConnector with
priority 0 for file:D:/Projects/karaf/assemblies/apache-karaf/target/assembly/system/
46 - org.ops4j.pax.url.mvn - 1.6.0 | Error resolving
artifactorg.apache.servicemix.specs:org.apache.servicemix.specs.activation-api-1.1:jar:1.9.0:Could
not find artifact
org.apache.servicemix.specs:org.apache.servicemix.specs.activation-api-1.1:jar:1.9.0
in defaultlocal (file:/C:/Users/xxxx/.m2/repository/)

通过此方案,如果发布包中的system目录包含了系统运行所需的所有依赖,则karaf将不再寻求从远程仓库加载依赖,从而实现离线部署。

通过上述方案实现karaf离线部署的同时产生了新的问题,如何确保system目录包含所有的依赖同时又不包含无用的冗余依赖?这个问题可以通过karaf-maven-plugin获得解决。下面来看一下配置:

<plugin><groupId>org.apache.karaf.tooling</groupId><artifactId>karaf-maven-plugin</artifactId><executions><execution><id>install-kar</id><phase>compile</phase><goals><goal>install-kars</goal></goals></execution><execution><id>features-add-to-repo</id><phase>generate-resources</phase><goals><goal>features-add-to-repository</goal></goals><configuration><descriptors><descriptor>mvn:org.apache.karaf.features/standard/${karaf.version}/xml/features</descriptor><descriptor>mvn:org.apache.karaf.features/spring/${karaf.version}/xml/features</descriptor>……                          </descriptors><features><feature>http</feature>   <feature>spring</feature>…</features><repository>target/assembly/system</repository></configuration></execution><execution><id>package</id><goals><goal>instance-create-archive</goal></goals></execution></executions>               </plugin>

通过此配置,系统在打包时,自动将karaf的standard feature库中的http feature及其依赖和spring feature库中的spring feature及其依赖下载到system目录中一起打包发布。

结论

通过上述方案,我们即可实现基于Karaf的项目自动打包、测试及发布。

基于Karaf的项目离线封装与发布相关推荐

  1. 基于 abp vNext 和 .NET Core 开发博客项目 - 终结篇之发布项目

    基于 abp vNext 和 .NET Core 开发博客项目 - 终结篇之发布项目 转载于:https://github.com/Meowv/Blog 既然开发完成了,还是拿出来溜溜比较好,本篇是本 ...

  2. 基于jsf的项目_JSF基于事件的交流:新派方法

    基于jsf的项目 在上一篇文章中 ,我们学习了基于Observer / Event Listener和Mediator模式的基于事件的通信. 由于它们的缺点,我想展示基于事件的通信的更有效方法. 我们 ...

  3. 基于iOS 10、realm封装的下载器

    代码地址如下: http://www.demodashi.com/demo/11653.html 概要 在决定自己封装一个下载器前,我本以为没有那么复杂,可在实际开发过程中困难重重,再加上iOS10和 ...

  4. Jenkins配置基于角色的项目权限管理--转

    本文将介绍如何配置jenkins,使其可以支持基于角色的项目权限管理. 由于jenkins默认的权限管理体系不支持用户组或角色的配置,因此需要安装第三发插件来支持角色的配置,本文将使用Role Str ...

  5. [转]基于Starling移动项目开发准备工作

    最近自己趁业余时间做的flash小游戏已经开发得差不多了,准备再完善下ui及数值后,投放到国外flash游戏站.期间也萌生想法,想把游戏拓展到手机平台.这两天尝试了下,除去要接入ane接口的工作,小游 ...

  6. 基于jsf的项目_JSF基于事件的沟通:过时的方法

    基于jsf的项目 用JSF编写的Web应用程序由相互交互的bean组成. 在开发Web应用程序时,bean之间的通信是主要的设计模式之一. 有时,一个bean需要将事件发送给其他bean,以通知它们某 ...

  7. 【基于Python+Flask项目部署系列--03】开发测试环境配置-基于Ubuntu16.04

    一.开发测试环境介绍 前提已经部署完[基于Python+Flask项目部署系列--02]Ubuntu16.04服务器安装.建议部署2套环境:开发测试环境+生产环境. 这篇文章主要讲解测试环境如何配置p ...

  8. 面向多媒体 linux 版本,基于MX Linux 的 AV Linux 新版发布,此AV非彼AV

    原标题:基于MX Linux 的 AV Linux 新版发布,此AV非彼AV 作者:Linux迷 链接:https://www.linuxmi.com/av-linux-2020-11-23-mx-l ...

  9. 基于表单数据的封装,泛型,反射以及使用BeanUtils进行处理

    在Java Web开发过程中,会遇到很多的表单数据的提交和对表单数据的处理.而每次都需要对这些数据的字段进行一个一个的处理就显得尤为繁琐,在Java语言中,面向对象的存在目的便是为了消除重复代码,减少 ...

最新文章

  1. python解压zip文件_python-29 python解压压缩包的几种方法
  2. android floatingactionbutton样式,Android 之 FloatingActionButton
  3. MySql 查询小数保留两位小数
  4. spring aop搭建(2) :基于代码的实现
  5. 你聚类个数是靠拍的?
  6. 剑指offer(21)从上往下打印二叉树
  7. java_version干什么的_java类中serialVersionUID的作用及其使用
  8. Avalanche发布AvalancheGo v1.2.3版本
  9. Windows 8 Beta 64位 简体中文 消费者预览版 安装截图(30P)
  10. resultset 的指针报错原因
  11. mysql 360怎么安装_mysql 5.7 安装配置方法图文教程
  12. Pow,Pos,Dpos共识机制比较
  13. vs code html插件_VS插件CodeRush全新发布v20.1.7|支持HTML
  14. U盘文件夹乱码无法删除的原因及解决方案
  15. pdf python 合同_Python实现将doc转化pdf格式文档的方法 如何用python代码生成pdf格式的合同文件...
  16. 地理位置数据存储方案——Redis GEO
  17. cycJava基础以及集合总结
  18. python 为女神编朵玫瑰花的代码_小伙利用Python绘制999种玫瑰花表白女神,会编程男孩子真好...
  19. 达人评测 R7 PRO 6850HS 核显相当于什么显卡
  20. 新安装Ubuntu 16.04 不能上网

热门文章

  1. python设置excel边框_python win32com excel边框格式
  2. HTTP协议-Cookie和Session详解
  3. Substrate 基础教程(Tutorials) -- 模拟网络 添加可信节点
  4. 基于C++实现两个分数的加减法
  5. 第三代测序技术在微生物研究中的应用
  6. Android传感器Motion Sensor开发实验
  7. 上海到底是一座什么样的城市?
  8. 深入理解自编码器(用变分自编码器生成图像)
  9. 《机器学习:实用案例解析》第三章 (3)
  10. Java链表(基本操作)