我刚刚开始使用OSGI开发,并且一直在努力理解处理依赖/第三方JAR或Maven依赖关系的最佳方法。在OSGi包中添加第三方maven依赖项的最佳方法
即,如果我正在创建一个包,那么可能性是我将使用几个第三方JAR。当我创建我的捆绑JAR以部署到OSGI时,显然这些第三方JAR不包含在内,因此捆绑将无法运行。
我知道一个选择是将这些JAR转换为捆绑包并将它们部署到OSGI容器。但是我不能为每一个我将要使用的maven依赖做这个。
什么是处理这种情况的最佳解决方案?有什么方法可以直接将jar文件嵌入到捆绑包中?
下面是我的Java主应用程序,它启动OSGi框架,然后尝试安装一个简单的包,它具有Log4j的依赖关系。而在将来,我可以有一些其他第三方的Maven的依赖以及..
public class OSGiTest {
private static Framework framework;
public static void main(String[] args) {
try {
FileUtils.deleteDirectory(new File("felix-cache"));
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
framework = frameworkFactory.newFramework(new HashMap<String, String>());
framework.start();
installAndStartBundle("DependencyBundle", "1.0.0");
} catch (IOException e) {
e.printStackTrace();
} catch (BundleException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void installAndStartBundle(final String name, final String version) throws BundleException, Exception {
final String basePath = "S:\\maven.repo\\dependency\\DependencyBundle\\1.0.0";
final BundleContext bundleContext = framework.getBundleContext();
final List<Bundle> installedBundles = new LinkedList<Bundle>();
BundleActivator b = new org.ops4j.pax.url.mvn.internal.Activator();
b.start(bundleContext);
String filename = name + ModelConstants.DASH + version + ModelConstants.DOTJAR;
String localFilename = ModelConstants.FILE_PROTOCOL + basePath+ File.separatorChar + filename;
installedBundles.add(bundleContext.installBundle(localFilename));
for (Bundle bundle : installedBundles) {
bundle.start();
}
}
}
下面是OSGi包DependencyBundle
我pom.xml文件 -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- POM Information about the Project -->
<modelVersion>4.0.0</modelVersion>
<groupId>com.host.personalize.goldeneye.dependency</groupId>
<artifactId>DependencyBundle</artifactId>
<version>1.0.0</version>
<!-- Packing Type is bundle for OSGI Library Bundle -->
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.cglib</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version><!--$NO-MVN-MAN-VER$ -->
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>4.3.0</version><!--$NO-MVN-MAN-VER$ -->
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- Build Configration -->
<build>
<plugins>
<!-- Apache Felix Bundle Plugin - For Generation of Manifest after Compile
phase -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<!-- Configuration for generating the Manifest.mf -->
<configuration>
<manifestLocation>src/main/resources/META-INF</manifestLocation>
<!-- Manifest Headers which need to customized during manifest generation -->
<instructions>
<Bundle-SymbolicName>DependencyBundle</Bundle-SymbolicName>
<Bundle-Activator>com.host.personalize.goldeneye.dependency.dependencybundle.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
不错的答案,卡拉夫还支持配置与功能文件的多个捆绑:http://karaf.apache.org/manual/latest-2.3.x/users-guide/provisioning.html – samlewis
感谢earcam的建议。现在写我正在使用Felix作为OSGi容器,如果我需要使用Karaf,那么我如何通过Java代码以编程方式启动OSGi容器?如果可能,你能否提供一个简单的例子,使用Apache Karaf作为OSGi容器并安装捆绑软件,就像我上面的用例一样? – ferhan
这可能会很困难,但是如果您打算从一开始就嵌入,那么您可以使用带有Felix/Equinox的最小依赖性(无?)的pax-url maven处理程序。 – earcam