繁体   English   中英

在OSGi捆绑包中添加第三方Maven依赖项的最佳方法

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM