Search code examples
javaosgiosgi-bundlekaraf

Handling 3rd party dependencies in OSGI


I'm trying to learn OSGI and figured I would build a simple rest application using Spark Servlet.

https://mvnrepository.com/artifact/com.sparkjava/spark-core/1.0

Within my maven build plugin, I embed Spark-Core. However, after I build and run the bundle, it tells me there is a wiring package problem. So I add the package import, rinse and repeat. I'll get a different wiring package problem, so then I add the dependency, etc.

This seems like a long tedious process to add one package after another. What's the correct way to do this?

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>osgi-demo</artifactId>
        <groupId>com.osgi-hacking</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>bundle</packaging>

    <artifactId>osgiclient</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>websocket-server</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>websocket-servlet</artifactId>
            <version>9.4.8.v20171121</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>
                            ${project.groupId}.${project.artifactId}
                        </Bundle-SymbolicName>
                        <Bundle-Name>
                            CUSTOM :: GREETER CLIENT :: BUNDLE
                        </Bundle-Name>
                        <Bundle-Version>
                            9.4.8.v20171121
                        </Bundle-Version>
                        <Bundle-Activator>
                            com.osgi.client.Activator
                        </Bundle-Activator>
                        <Embed-Dependency>
                            spark-core
                        </Embed-Dependency>
                        <Import-Package>
                            *
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Solution

  • The spark core jar is already a bundle. So there is no need to embed it. Simply install it as well as its dependendencies (which is mainly jetty) in OSGi.

    For your own bundle. Simply remove all inside . The defaults of the maven bundle plugin will produce what you need.

    It will detect the packages you need and write Import-Package statements for them. When you then have spark-core installed as a bundle it should work fine.