蜀川居 2012-07-11
FAB Project是以pom.xml配置文件为主进行该JAR文件的依赖解析的,因此可以确保部署时期本JAR对第三方JAR文件
的依赖问题。
1. Generating a FAB Project
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-blueprint -DarchetypeVersion=2.9.0.fuse-7-060 -DgroupId=GroupId -DartifactId=ArtifactId -Dversion=Version
进入到上面命令生成的Aritfactid文件夹下,执行:mvn install命令将在targst目录下生成JAR文件。
2. Class Sharing
FAB默认情况下是将每个Maven 依赖在运行期间添加至自己的私有的Class Space中的,这样的优点:
可以避免统一依赖JAR的版本部一致问题;缺点是JVM需要在内存中维护每一个依赖JAR文件的Copy.
解决上述问题,有下面两个方案:
(1) 使用scope指定依赖的范围
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camel-version}</version> <scope>provided</scope> </dependency>
(2) 使用Manifest.MF Header指定共享的依赖
例如,共享org.apache.camel GroupId下的所有AritfactId:
FAB-Provided-Dependency: org.apache.camel:*
3. 修改一个Maven Project让其适合FAB部署
(1)确保JAR文件的packaging 格式是:JAR
<project ... > ... <packaging>jar</packaging> ... </project>
(2) 指定JDK编译版本,仅仅指定JAVA_HOME和PATH环境变量是不够的
<project .> ... <build> <defaultGoal>install</defaultGoal> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> ... </project>
(3)配置可分享的类或JAR,指定其scope为provided
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http</artifactId> <version>2.9.0.fuse-7-060</version> <scope>provided</scope> </dependency>
(4) 将测试期间需要的依赖指定为test
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j-version}</version> <scope>test</scope> </dependency
(5) 针对WS Application使用cxf-bundle
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle</artifactId> <version>2.5.0.fuse-7-060</version> <scope>provided</scope> </dependency
默认情况下cxf-bundle包含所有的CXF Bundle.
(6) 优先考虑使用Blueprin而不是Spring, 因为Blueprint是松耦合的,而Spring XML中引入的依赖FAB在部署期间易发生
ClassNotFound Exception.
(7) 使用Mainfest.MF Header配置可选的依赖,可使用通配符:*
FAB-Version-Range-Digits: 2 FAB-Provided-Dependency: org.apache.camel:*
4. Configuring a FAB
针对FAB部署,可在Mnifext.MF文件中添加许多Header:
(1) 指定版本范围
例如下面的pom中的依赖,生成后的JAR文件中的Mainfest.MF文件为:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.8.1-fuse-00-02</version> <scope>provided</scope> </dependency>
Import-Package: org.apache.camel;version="[2.8.1-fuse-00-02,2.9)"
指定版本范围策略,可通过Header: FAB-Version-Range-Digits: 2,可选值有:0,1,2,3,4
(2) 指定共享的依赖
FAB-Provided-Dependency: groupId1:artifactId1 groupId2:artifactId2 ...
例如:FAB-Provided-Dependency: org.apache.camel:* org.springframework:*
(3) 指定可选的依赖:
在pom.xml文件添加:<optional>true</optional>
或添加Header: FAB-Include-Optional-Dependency: *:*
(4) 指定OSGi 需要的Bundle:
FAB-Dependency-Require-Bundle: groupId1:artifactId1 groupId2:artifactId2 ...
(5) 指定不需要的依赖:
FAB-Exclude-Dependency: log4j logkit
(6) 客制化导入:
Import-Package: com.acme.special;version="[1.0,1.1)"
(7) Automatic feature detection
FAB-Skip-Matching-Feature-Detection: org.apache.camel
(8) Requiring features
FAB-Require-Feature-URL: mvn:org.apache.cxf.karaf/apache-cxf/2.5.0.fuse-7-060/xml/features FAB-Require-Feature: cxf-sts
(9) Respecting pre-installed features and bundles
FAB-Install-Provided-Bundle-Dependencies: true
5. 配置Mainfest.MF文件Header
(1) 在pom文件中:
<project .> ... <build> ... <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin </artifactId> <configuration> <archive> <index>true</index> <manifestEntries> <FAB-Version-Range-Digits>0</FAB-Version-Range-Digits> <FAB-Provided-Dependency> org.apache.camel:* org.apache.cxf:* org.apache.activemq:* </FAB-Provided-Dependency> </manifestEntries> </archive> </configuration> </plugin> ... </plugins> </build> </project>
(2) 在META-INF/MANIFEST.MF文件中:
<project .> ... <build> ... <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin </artifactId> <configuration> <!-- lets use the default META-INF/MANIFEST.MF if its there --> <useDefaultManifestFile>true</useDefaultManifestFile> </configuration> </plugin> ... </plugins> </build> </project>
6. Deploying a FAB
FAB是用Maven构建工具创建的一个JAR文件,在这个JAR文件中有一个位于下面目录结构的pom.xml文件:
META-INF/maven/groupId/artifactId/pom.xml,有两种部署方式:Hot Deploy和手工部署。
(1) 安装FAB及私有的依赖
这种方式与传统的WAR部署方式没什么打的区别,唯一的区别是FAB不需要和它的依赖部署在一起。
(2) 安装FAB及共享的依赖
这种方式与使用OSGi的命令安装Bundle没什么区别,并可以利用OSGi的Bundles和Features特性,及根据Features
特性自动安装需要的JAR。
(3) 安装FAB有私有的依赖也有共享的依赖
这种安装FAB的方式在FAB启动时只会启动FAB私有的依赖,而不会启动共享的依赖。
Hot Deploy:
将JAR 文件Copy 至安装目录下的deploy目录下。
Manual Deploy:
(1) 使用FAB scheme: osgi:install fab:mvn:groupId/artifactId/version 或fab:file:PathName 或fab:http:Host[:Port]/[Path]
(2) 启动FAB:fab:start 180 #180FAB ID
(3) 停止FAB:fab:stop 180
(4) 卸载FAB:fab:uninstall 180
7. Configuring Maven for FAB
配置文件位于:InstallDir/etc/org.ops4j.pax.url.mvn.cfg
客制化定义本地Repository:
# 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:~/.m2/repository
客制化定义远程Repository:
org.ops4j.pax.url.mvn.repositories= \ http://repo1.maven.org/maven2, \ http://repo.fusesource.com/nexus/content/repositories/releases, \ http://repo.fusesource.com/nexus/content/repositories/snapshots@snapshots@noreleases, \ http://repository.apache.org/content/groups/snapshotsgroup@snapshots@noreleases, \ http://svn.apache.org/repos/asf/servicemix/m2-repo, \ http://repository.springsource.com/maven/bundles/release,\ http://repository.springsource.com/maven/bundles/external