[英]Best way to add Third party maven dependencies in OSGi bundle
我剛剛開始OSGI開發,正在努力了解什么是處理依賴/第三方JAR或Maven依賴的最佳方法。
即,如果我要創建捆綁包,那么可能是我將使用一些第三方JAR。 當我創建要部署到OSGI的捆綁軟件JAR時,顯然不包括這些第三方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>
如果您才剛剛開始,為什么要通過管理框架運行時的復雜性來深入研究?
采取更簡單,可以說更短的路線,並從諸如Apache Karaf之類的預構建運行時開始,您可以在命令行中使用pax-url項目的maven url處理程序簡單地安裝捆綁包,也可以使用wrap:協議來動態為依賴項添加有效的OSGi清單。
一旦知道了使用某項功能后,您就可以了解並構建自己的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.