简体   繁体   中英

Best way to add Third party maven dependencies in OSGi bundle

I have just started with OSGI development and am struggling to understand what is the best way to handle dependent/third party JARs or maven dependencies.

ie if I'm creating a bundle then the likelyhood is that I will be using a few 3rd party JARs. When I create my bundle JAR to deploy to OSGI, obviously these 3rd party JARs are not included and thus the bundle will not run.

I understand that one option is to turn these JARs into bundles and also deploy them to the OSGI container. But I cannot do this for each and every maven dependency that I am going to use.

What is the best solution to handle this kind of situtation? Is there any way I can embed the jar file directly to bundles?

Below is my java main application that is starting the OSGi framework and then trying to install a simple bundle which has a dependency of Log4j. And in future, I can have some other third party maven dependency as well..

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();
        }
    }
}

Below is my pom.xml file for OSGi bundle 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>

If you are just starting why jump in at the deep end by managing the intricacies of the framework runtime?

Take an easier, and arguably shorter route, and begin with a pre-build runtime such as Apache Karaf , you can simply install bundles using the pax-url project's maven url handler from the command line , you can also use the wrap: protocol to dynamically add valid OSGi manifests for dependencies.

Once you know what your doing using one, then you can learn about and build your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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