在OSGi包中添加第三方Maven依赖关系的最佳方法

我刚刚开始使用OSGI开发,并且正在努力理解处理依赖/第三方JAR或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.repodependencyDependencyBundle1.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:protocol为依赖性动态添加有效的OSGi清单。

一旦你知道自己在做什么,那么你可以学习和建立你自己的。

链接地址: http://www.djcxy.com/p/74757.html

上一篇: Best way to add Third party maven dependencies in OSGi bundle

下一篇: OSGi bundle repository with plain Maven JARs