4

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.

Crawler
  • 1,988
  • 3
  • 21
  • 42
  • Special reason using such an ancient version of [maven-bundle-plugin?](http://search.maven.org/#search|gav|1|g%3A%22org.apache.felix%22%20AND%20a%3A%22maven-bundle-plugin%22) – khmarbaise Dec 04 '15 at 12:07
  • Did you look at wrapping the dependency ? http://stackoverflow.com/questions/4565327/good-way-to-wrap-jars-for-osgi-with-maven – stalet Dec 04 '15 at 12:09

4 Answers4

3

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.

Crawler
  • 1,988
  • 3
  • 21
  • 42
2

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.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • +1 for your OSGi ready jdbc suggestion. I followed your blog n it works great. But I also need to add other dependencies which are not OSGi ready such as org.jdbi. Can u suggest me jdbi that is already OSGi ready or method to make non OSGi ready dependencies OSGi ready. – Crawler Dec 07 '15 at 04:45
0

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.

Souciance Eqdam Rashti
  • 3,143
  • 3
  • 15
  • 31
  • I fixed jdbc problem but now i need to work with jdbi. jdbi is shown active in karaf console OSGi bundle list. but issue is not solved. – Crawler Dec 07 '15 at 05:05
0

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.

Pavel Vlasov
  • 131
  • 5
  • I have to use org.jdbi. org.skife.jdbi.* package throws wiring exception and above package need org.antlr.stringtemplate package. now can you please clearify how to create wrapper for this condition. I tried to create wrapper but its not working. – Crawler Dec 07 '15 at 05:08
  • This is the process which I follow when wrapping maven components into OSGi: – Pavel Vlasov Dec 15 '15 at 15:51
  • a) Look at the dependency tree and then look at Eclipse and Orbit repositories to see if there are already wrapped dependencies (e.g. org.json, javax.transaction, junit, ...) b) If a number of dependencies is small and they are not available at Eclipse and Orbit repositories - try to use p2-maven-plugin. In some cases it doesn't work well. c) If there are existing wrapped dependencies or p2-maven-plugin doesn't work well (e.g. in the case of Selenium WebDriver) - create a plugin project in Eclipse with all unwrapped dependency jars, add wrapped dependencies and build a repo with Tycho/Maven – Pavel Vlasov Dec 15 '15 at 15:58
  • You can use my third-party repository as an example: www.nasdanika.org/third-party.zip – Pavel Vlasov Dec 15 '15 at 16:07