Make maven dependency OSGi ready

7k Views Asked by At

Im trying to connect database in JFUSE project. I've included com.mysql.jdbc dependency in pom file and project build runs fine. But then I encounter this annoying problem. When I try to install the bundle to OSGi, installation failed with following error:

Unable to start bundle mvn:com.info.demo/demo-rest/1.0: Unresolved const raint in bundle com.info.demo.rest [363]: Unable to resolve 363.0: missing requirement [363.0] osgi.wiring.package; (osgi.wiring.package=com.mysql.jdbc)

I tried all available solution from SO, but they didnt solve the issue. While I was trying to find cause of error, I saw warning in dependency declaration of mysql in IDE that says:

Maven Dependency is not OSGi ready

So, I guess main reason is that my dependency is not ready for OSGi container. Can any one help me how to make maven dependency OSGi ready?

Below is my pom.xml code:

<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/maven-v4_0_0.xsd">

***Project specific declarations here***

<build>    
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.1.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Fragment-Host>org.springframework.jdbc</Fragment-Host>
                    <Import-Package>com.mysql.jdbc</Import-Package>
                </instructions>
            </configuration>                
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.mysql.jdbc</groupId>
        <artifactId>com.springsource.com.mysql.jdbc</artifactId>
        <version>5.1.6</version>
    </dependency>
   ***Other Dependencies***
</dependencies>

Edit: I followed Christain suggestion and it work great. But I need to add other dependencies which are not OSGi ready.

I went through installing non OSGi dependencies to FUSE server. And also wrapping dependencies but didnt solved issue.

Please help me with details solution, Im really stuck here.

4

There are 4 best solutions below

0
On BEST ANSWER

With some days of searching I finally found the simplest solutions. Non OSGi bundle can be made OSGi ready by just using this simple osgi wrap command in Karaf or ServiceMix terminal:

osgi:install wrap:mvn:org.jdbi/jdbi/2.70

Dependency will be installed in Fuse server which can be verified by using command.

osgi:list

Now just add dependencies in pom.

<dependency>
    <groupId>org.jdbi</groupId>
    <artifactId>jdbi</artifactId>
    <version>2.70</version>
</dependency>

Idea will still warn you dependency is not OSGi ready, just ignore it.

Finally import needed package in maven bundle plugin and you are done.

<plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>${version.maven-bundle-plugin}</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Import-Package>
                        org.skife.jdbi.v2,
                        org.skife.jdbi.v2.util,
                        org.skife.jdbi.cglib.proxy,
                        org.skife.jdbi.v2.sqlobject.stringtemplate,
                        org.skife.jdbi.v2.sqlobject,
                        org.skife.jdbi.cglib.core
                    </Import-Package>
                </instructions>
            </configuration>
        </plugin>

Hope this will help somebody in future.

1
On

That error means that Fuse does not know about the bundle for mysql driver. You need to manually install it. Since the mysql connector is in bundle form it is easier to install it in an OSGI environment. Just use osgi:install and then the name of the bundle.

You can also build a Kar archive to avoid this.

Alternatively use Karaf/Fuse own sql driver as mentioned by Christian.

1
On

In the case of the mysql driver you do not have to do the wrapping yourself. The mysql connector j that is available from maven central is already OSGi ready. The maven coordinates are mvn:mysql/mysql-connector-java/5.1.34.

As it is not easy to use a plain jdbc driver in OSGi I recommend that you also use pax-jdbc. It allows to create a DataSource as an OSGi service by just supplying some config.

You can install it this way (In Karaf > 3): feature:repo-add pax-jdbc 0.7.0 feature:install transaction pax-jdbc-mysql pax-jdbc-config pax-jdbc-pool-dbcp2

This already provides the DataSourceFactory for MySQL and enables the config and pooling support.

Then you just have to create a config like described on the pax jdbc docs.

4
On

For Maven dependencies which are not OSGi ready there is a maven plugin to warp them - https://github.com/reficio/p2-maven-plugin, a tutorial - http://www.vogella.com/tutorials/EclipseTycho/article.html#d314078e1011. This approach works well for simple components, but for components with lots of dependencies or lots of exported packages you might need to manually create a wrapper bundle, e.g. I had to manually create wrapper bundles for pegdown, selenium and selendroid.