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:
- 查找配置参数
- 如果未找到,查找 ${user.home}/.m2/settings.xml
- 如果未找到,查找 ${maven.home}/conf/settings.xml
- 如果未找到,查找 ${M2_HOME}/conf/settings.xml
Local repository
Maven本地仓库的查找顺序如下:
- 查找配置参数定义;
- 如果未找到,在settings.xml中进行查找
- 如果未找到,默认认为仓库位于${user.home}/.m2/repository.
Repositories
Maven仓库的解析顺序如下:
- 在配置参数中查找
- 在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支持多种部署包格式,如Karaf归档包(Kar),方便用户打包和分发应用。 - 为了保护系统的安全,Karaf提供了完整的安全机制,包括用户认证和授权。 - 配置命令让管理员能够灵活地配置和管理Karaf实例。 - ...
Karaf是Apache软件基金会的一个项目,是一个基于OSGi的轻量级运行时容器,用于部署和管理服务。在Karaf容器中集成Servlet并发布REST接口是一项常见的任务,下面我们将详细讨论这个过程。 首先,我们需要理解OSGi与...
5. **部署流程**:掌握如何将Java项目打包为OSGi捆绑包,并在Karaf中部署和运行。 6. **DS服务**:学习声明式服务的使用,以声明式方式定义和管理服务。 7. **Karaf事件系统**:了解如何利用事件系统进行组件间的...
Apache Karaf是一款轻量级的Java企业服务框架,基于OSGi规范,用于构建、管理和部署分布式应用程序。在本文中,我们将深入探讨Karaf的运行环境、内置命令、骨架使用,以及bundle和feature的开发与部署流程。同时,...
此外,源码还可以帮助你了解Karaf与其他技术(如Spring、JMS等)的集成方式。 在阅读“learning Apache Karaf.pdf”时,结合源码实践,你将能够掌握如何有效地利用Apache Karaf构建和管理复杂的应用程序生态系统,...
Apache Karaf是基于OSGi(Java模块化系统)的轻量级开源应用服务器,它为开发者提供了一个可扩展且灵活的运行环境。标题中的"apache-karaf-4.0.0"指的是Apache Karaf的4.0.0版本,这通常意味着一个重要的软件更新或...
7. **Maven集成**:Karaf与Maven紧密集成,使得开发者可以方便地利用Maven仓库来部署和管理OSGi捆绑包。 8. **云就绪**:由于其模块化和可扩展性,Apache Karaf是云平台的理想选择,可以作为微服务的基础容器。 9....
Karaf是Apache旗下的一个开源项目.Karaf同时也是一个基于OSGi的运行环境,Karaf提供了一个轻量级的OSGi容器,可以用于部署 各种组件,应用程序.Karaf提供了很多特性用于帮助开发者和用户更加灵活的部署应用,例如:热部署...
### Karaf 手册与入门指南 #### 概览 Apache Karaf 是一款基于 OSGi 的小型运行时环境,它提供了一个轻量级的容器,可在其中部署各种组件和应用。以下是 Karaf 支持的主要特性: 1. **热部署(Hot Deployment)**...
Apache Karaf是一个开源的、基于OSGi(Open Service Gateway Initiative)规范的运行环境,它为开发者提供了一个轻量级的服务容器。Karaf这个名字来源于阿拉伯语,意为“小船”,象征着它承载并运载应用服务的能力。...
7. **集成性**:Apache Karaf与许多其他开源项目有良好的集成,如Spring DM、Camel、Felix等,广泛应用于企业级解决方案。 对于“apache-karaf-4.0.2.tar.gz”的安装和解压,可以遵循以下步骤: 1. **下载**:首先...
如果你的应用是基于 Maven 构建的,可以在项目的 POM 文件中添加以下依赖: ```xml xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>...
Karaf是Apache旗下的一个开源项目.Karaf同时也是一个基于OSGi的运行环境,Karaf提供了一个轻量级的OSGi容器,可以用于部署各种组件,应用程序.Karaf提供了很多特性用于帮助开发者和用户更加灵活的部署应用,例如:热部署,...
Apache Karaf是Apache ServiceMix项目的一个组件,主要设计用于快速部署和管理OSGi服务。它提供了一个命令行界面和Web管理控制台,便于开发者操作和管理OSGi容器。 2. **OSGi**: OSGi(Open Services Gateway ...
1. **理解Karaf Cellar的基本概念**:包括集群的概念、Cellar的核心机制以及与其他Karaf子项目的关系。 2. **安装和配置Karaf Cellar**:详细步骤介绍如何在现有的Karaf环境中安装并配置Cellar插件,为后续的集群...
此外,Karaf提供了基于图形界面的管理工具(如Karaf Console),使得Windows用户也能方便地进行管理操作。 总结,Apache Karaf 2.3.4是针对Windows平台的OSGi容器,它提供了一个强大的开发和运行环境,支持模块化...
[Packt Publishing] Apache Karaf 学习教程 (英文版) [Packt Publishing] Learning Apache Karaf (E-Book) ☆ 图书概要:☆ Develop and deploy applications using the OSGi-based runtime container, Apache ...
《Karaf Decanter 示例详解与实践》 Apache Karaf 是一个轻量级的Java OSGi容器,它为开发人员提供了快速构建、部署和管理Java应用程序的平台。Decanter是Karaf的一个模块,专注于监控和日志管理,提供了一套全面的...