最近使用karaf+Jetty使用feature碰到了很多问题

介绍如何配置Karaf以实现离线打包与发布,避免依赖远程仓库。

http://www.itpub.net/thread-1776847-1-1.html JPA资料

http://springsfeng.iteye.com/blog/1392658 feature参考

http://blog.youkuaiyun.com/caolaosanahnu/article/details/7933333

粗心导致排错搞了两天,太悲催了,Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.npn.NextProtoNego$ServerProvider的异常出现,是由于在karaf中npn-boot的版本设置错误了。。。。。。

set JAVA_OPTS=%JAVA_OPTS% -Djetty.home=%KARAF_BASE%/jetty -Xbootclasspath/p:npn-boot-${npn.boot.version}.jar



基于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协议:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. mvn-uri := 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ]  
  2. repository-url := < rfc2396 uri > ; an url that points to a maven 2 repository  
  3. group-id := < maven groupId > ; group id of maven artifact  
  4. artifact-id := < maven artifactId > ; artifact id of maven artifact  
  5. version := < maven version > | 'LATEST' | range ; version of maven artifact  
  6. range := ( '[' | '(' ) version ',' version ( ')' | ']' )  
  7. type := < maven type> ; type of maven artifact  
  8. 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]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <metadata modelVersion="1.1.0">  
  3.   <groupId>org.ops4j.pax.web</groupId>  
  4.   <artifactId>pax-web-features</artifactId>  
  5.   <version>3.0.4-SNAPSHOT</version>  
  6.   <versioning>  
  7.     <snapshot>  
  8.       <timestamp>20131113.170334</timestamp>  
  9.       <buildNumber>2</buildNumber>  
  10.     </snapshot>  
  11.     <lastUpdated>20131113170334</lastUpdated>  
  12.     <snapshotVersions>  
  13.       <snapshotVersion>  
  14.         <extension>pom</extension>  
  15.         <value>3.0.4-20131113.170334-2</value>  
  16.         <updated>20131113170334</updated>  
  17.       </snapshotVersion>  
  18.       <snapshotVersion>  
  19.         <classifier>features</classifier>  
  20.         <extension>xml</extension>  
  21.         <value>3.0.4-20131113.170334-2</value>  
  22.         <updated>20131113170334</updated>  
  23.       </snapshotVersion>  
  24.       <snapshotVersion>  
  25.         <classifier>jettyconfig</classifier>  
  26.         <extension>xml</extension>  
  27.         <value>3.0.4-20131113.170334-2</value>  
  28.         <updated>20131113170334</updated>  
  29.       </snapshotVersion>  
  30.     </snapshotVersions>  
  31.   </versioning>  
  32. </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文件:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #  
  2. # If set to true, the following property will not allow any certificate to be used  
  3. # when accessing Maven repositories through SSL  
  4. #  
  5. #org.ops4j.pax.url.mvn.certificateCheck=  
  6.    
  7. #  
  8. # Path to the local Maven settings file.  
  9. # The repositories defined in this file will be automatically added to the list  
  10. # of default repositories if the 'org.ops4j.pax.url.mvn.repositories' property  
  11. # below is not set.  
  12. # The following locations are checked for the existence of the settings.xml file  
  13. #   * 1. looks for the specified url  
  14. #   * 2if not found looks for ${user.home}/.m2/settings.xml  
  15. #   * 3if not found looks for ${maven.home}/conf/settings.xml  
  16. #   * 4if not found looks for ${M2_HOME}/conf/settings.xml  
  17. #  
  18. #org.ops4j.pax.url.mvn.settings=  
  19.    
  20. #  
  21. # Path to the local Maven repository which is used to avoid downloading  
  22. # artifacts when they already exist locally.  
  23. # The value of this property will be extracted from the settings.xml file  
  24. # above, or defaulted to:  
  25. #     System.getProperty( "user.home" ) + "/.m2/repository"  
  26. #  
  27. #org.ops4j.pax.url.mvn.localRepository=  
  28.    
  29. #  
  30. # Default this to false. It's just weird to use undocumented repos  
  31. #  
  32. org.ops4j.pax.url.mvn.useFallbackRepositories=false  
  33.    
  34. #  
  35. # Uncomment if you don't wanna use the proxy settings  
  36. # from the Maven conf/settings.xml file  
  37. #  
  38. # org.ops4j.pax.url.mvn.proxySupport=false  
  39.    
  40. #  
  41. # Disable aether support by default.  This ensure that the defaultRepositories  
  42. # below will be used  
  43. #  
  44. #org.ops4j.pax.url.mvn.disableAether=true  
  45.    
  46. #  
  47. # Comma separated list of repositories scanned when resolving an artifact.  
  48. # Those repositories will be checked before iterating through the  
  49. #    below list of repositories and even before the local repository  
  50. # A repository url can be appended with zero or more of the following flags:  
  51. #    @snapshots  : the repository contains snaphots  
  52. #    @noreleases : the repository does not contain any released artifacts  
  53. #  
  54. # The following property value will add the system folder as a repo.  
  55. #  
  56. #org.ops4j.pax.url.mvn.defaultRepositories=  
  57.    
  58. # Use the default local repo (e.g.~/.m2/repository) as a "remote" repo  
  59. org.ops4j.pax.url.mvn.defaultLocalRepoAsRemote=false  
  60.    
  61. #  
  62. # Comma separated list of repositories scanned when resolving an artifact.  
  63. # The default list includes the following repositories containing releases:  
  64. #    http://repo1.maven.org/maven2  
  65. #    http://repository.apache.org/content/groups/snapshots-group  
  66. #    http://svn.apache.org/repos/asf/servicemix/m2-repo  
  67. #    http://repository.springsource.com/maven/bundles/release  
  68. #    http://repository.springsource.com/maven/bundles/external  
  69. # To add repositories to the default ones, prepend '+' to the list of repositories  
  70. # to add.  
  71. # A repository url can be appended with zero or more of the following flags:  
  72. #    @snapshots  : the repository contains snaphots  
  73. #    @noreleases : the repository does not contain any released artifacts  
  74. #    @id=reponid : the id for the repository, just like in the settings.xml this is optional but recomendet  
  75. #  
  76. # The default list doesn't contain any repository containing snapshots as it can impact the artifacts resolution.  
  77. # You may want to add the following repositories containing snapshots:  
  78. #    http://repository.apache.org/content/groups/snapshots-group@id=apache@snapshots@noreleases  
  79. #    http://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@norelease  
  80. #    http://oss.sonatype.org/content/repositories/ops4j-snapshots@id=ops4j.sonatype.snapshots.deploy@snapshots@noreleases  
  81. #  
  82. org.ops4j.pax.url.mvn.repositories= \  
  83.     http://repo1.maven.org/maven2@id=central, \  
  84.     http://repository.springsource.com/maven/bundles/release@id=spring.ebr, \  
  85.     file:${karaf.home}/${karaf.default.repository}@id=system.repository, \  
  86.     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发布程序即可实现离线部署。

具体配置调整如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Path to the local Maven settings file.  
  2. # The repositories defined in this file will be automatically added to the list  
  3. # of default repositories if the 'org.ops4j.pax.url.mvn.repositories' property  
  4. # below is not set.  
  5. # The following locations are checked for the existence of the settings.xml file  
  6. #   * 1. looks for the specified url  
  7. #   * 2if not found looks for ${user.home}/.m2/settings.xml  
  8. #   * 3if not found looks for ${maven.home}/conf/settings.xml  
  9. #   * 4if not found looks for ${M2_HOME}/conf/settings.xml  
  10. #  
  11. <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(255, 102, 0);">org.ops4j.pax.url.mvn.settings=</span></span>  
  12.    
  13. #  
  14. # Path to the local Maven repository which is used to avoid downloading  
  15. # artifacts when they already exist locally.  
  16. # The value of this property will be extracted from the settings.xml file  
  17. # above, or defaulted to:  
  18. #     System.getProperty( "user.home" ) + "/.m2/repository"  
  19. #  
  20. <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(255, 102, 0);">org.ops4j.pax.url.mvn.localRepository=file:${karaf.home}/${karaf.default.repository}@id=system.repository@snapshots</span></span>  
  21.    
  22. # Use the default local repo (e.g.~/.m2/repository) as a "remote" repo  
  23. <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(255, 102, 0);">org.ops4j.pax.url.mvn.defaultLocalRepoAsRemote=true</span></span>  
  24.    
  25. #  
  26. # Comma separated list of repositories scanned when resolving an artifact.  
  27. # The default list includes the following repositories:  
  28. #    http://repo1.maven.org/maven2@id=central  
  29. #    http://repository.apache.org/content/groups/snapshots-group@id=apache@snapshots@noreleases  
  30. #    http://repository.springsource.com/maven/bundles/release@id=spring.ebr  
  31. #    http://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@norelease  
  32. #    http://oss.sonatype.org/content/repositories/ops4j-snapshots@id=ops4j.sonatype.snapshots.deploy@snapshots@noreleases  
  33. #    file:${karaf.home}/${karaf.default.repository}@id=system.repository  
  34. #    file:${karaf.data}/kar@id=kar.repository@multi  
  35. # To add repositories to the default ones, prepend '+' to the list of repositories  
  36. # to add.  
  37. # A repository url can be appended with zero or more of the following flags:  
  38. #    @snapshots  : the repository contains snapshots  
  39. #    @noreleases : the repository does not contain any released artifacts  
  40. #    @id=repository.id : the id for the repository, just like in the settings.xml this is optional but recommended  
  41. #  
  42. <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(255, 102, 0);">org.ops4j.pax.url.mvn.repositories= \  
  43.     file:${karaf.home}/${karaf.default.repository}@id=system.repository@snapshots, \  
  44.     file:${karaf.data}/kar@id=kar.repository@multi</span></span>  

 

 

在此模式下,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获得解决。下面来看一下配置:

[xml]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <plugin>  
  2.                 <groupId>org.apache.karaf.tooling</groupId>  
  3.                 <artifactId>karaf-maven-plugin</artifactId>  
  4.                 <executions>  
  5.                     <execution>  
  6.                         <id>install-kar</id>  
  7.                         <phase>compile</phase>  
  8.                         <goals>  
  9.                             <goal>install-kars</goal>  
  10.                         </goals>  
  11.                     </execution>  
  12.                     <execution>  
  13.                         <id>features-add-to-repo</id>  
  14.                         <phase>generate-resources</phase>  
  15.                         <goals>  
  16.                             <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(255, 102, 0);"><goal>features-add-to-repository</goal></span></span>  
  17.                         </goals>  
  18.                         <configuration>  
  19.                             <descriptors>  
  20.                                 <<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(0, 0, 255);">descriptor>mvn:org.apache.karaf.features/standard/${karaf.version}/xml/features</descriptor>  
  21.                                 <descriptor>mvn:org.apache.karaf.features/spring/${karaf.version}/xml/features</descriptor></span></span>  
  22.                                 ……                            
  23.                             </descriptors>  
  24.                             <features>  
  25.                                 <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(0, 0, 255);"><feature>http</feature>     
  26.                                 <feature>spring</feature></span></span>  
  27.                                 …  
  28.                             </features>  
  29.                             <span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; list-style: none outside none; word-break: normal; word-wrap: break-word; color: rgb(128, 0, 128);"><repository>target/assembly/system</repository></span></span>  
  30.                         </configuration>  
  31.                     </execution>  
  32.                     <execution>  
  33.                         <id>package</id>  
  34.                         <goals>  
  35.                             <goal>instance-create-archive</goal>  
  36.                         </goals>  
  37.                     </execution>  
  38.                 </executions>                 
  39.             </plugin>  

 

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

结论

 

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


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值