我刚刚开始进行 OSGI 开发,并且正在努力了解处理依赖/第三方 JAR 或 maven 依赖项的最佳方法是什么。
即,如果我正在创建一个捆绑包,那么我可能会使用一些第 3 方 JAR。当我创建我的包 JAR 以部署到 OSGI 时,显然这些 3rd 方 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 包的 pom.xml 文件DependencyBundle
-
<?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>