Handling 3rd party dependencies in OSGI - java

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>

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.

Related

package org.apache.hadoop.hive.ql.exec.vector does not exist

I'm trying to work with Hive UDF's and UDAF's and so far, I've managed quite well in doing so using maven from the command line on a VM that has essentially been set up quite well by someone very knowledgable. I'm new to Java (projects, not so much the language) but I can at least see what my current issue is. What I don't know is why it's happening and how to fix it.
So...
my problem is that for whatever reason, when running
mvn install
the compiler can't find any classes past
/org/apache/hadoop/hive/ql/exec
It can find all classes in the 'exec' folder, but nothing in any subfolders nested in 'exec'. I don't understand why this is the case. Here is my POM file.
<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">
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.matthewrathbone.example.RawMapreduce</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modelVersion>4.0.0</modelVersion>
<groupId>com.matthewrathbone.example</groupId>
<artifactId>hive-extensions</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>hive-extensions</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.0.0-mr1-cdh4.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>0.10.0-cdh4.3.1</version>
<scope>provided</scope>
</dependency>
<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-test</artifactId>
<version>2.0.0-mr1-cdh4.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
</project>
An example of one of the many errors I get is in the title. 'vector' is a folder located in /org/apache/hadoop/hive/ql/exec.
As far as I can tell, the hive-exec.jar file is listed as a dependency in my POM file, so I'm not really sure why the compiler is having trouble locating all the classes contained within it (it can find some, as mentioned previously).
Essentially what I'm trying to do is incorporate one of the built-in UDAF's as my own temporary function, so that I can bypass Hive's restrictions on sub-querying inside the 'SELECT' statement.
So, to be more specific, I made some adjustments and eventually narrowed it down to compiler was looking for class VectorizedUDAFs which as it turns out, my version of hive-exec .jar does not have.... go figure. Thus, it couldn't find it because it actually did not exist...

Maven EAR multimodule project not packaging persistence.xml

I`m working in a EAR project with Maven which has 2 modules. Images speaks louder than words, so let me show you the structure:
Parent pom project and modules
sigea-model contains model, repository and service layers (The "M" in MVC). sigea-web contains web pages and controller beans (VC) and sigea-ear is just a wrapper to package the other 2 modules in a EAR package.
Configuration files in modules
As you can see, sigea-ear has an empty META-INF folder. Both beans.xml files in sigea-model and sigea-web are just empty marker files because AFAIK, CDI by default search in all annotated classes (but this is not the problem right now). persistence.xml is a simple file which uses JTA transactions with a connection pool (which is working because I ping from the Glassfish's admin console and is successful).
Finally, when I package the application I get the following:
As you can see, there's no persistence.xml. All this came out because I deployed the application successfully but in the first click I got the Exception
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
...
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
Here are my pom files:
pom.xml[sigea-app] (parent project)
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>ar.edu.unt.sigea</groupId>
<artifactId>sigea-app</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>sigea-model</module>
<module>sigea-web</module>
<module>sigea-ear</module>
</modules>
<build>
<pluginManagement>
...
</pluginManagement>
<plugins>
<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>
<dependencyManagement>
<!-- I suppress some lines for brevity -->
<dependencies>
<dependency>
<artifactId>sigea-model</artifactId>
</dependency>
<dependency>
<artifactId>sigea-model</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<artifactId>sigea-model</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
</dependencies>
</project>
pom.xml[sigea-ear]
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ar.edu.unt.sigea</groupId>
<artifactId>sigea-app</artifactId>
<version>1.0</version>
</parent>
<artifactId>sigea-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<artifactId>sigea-model</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<artifactId>sigea-web</artifactId>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib/</defaultLibBundleDir>
<skinnyWars>true</skinnyWars>
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>sigea-web</artifactId>
<contextRoot>/sigea</contextRoot>
</webModule>
<ejbModule>
<groupId>${project.groupId}</groupId>
<artifactId>sigea-model</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.xml[sigea-web]
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>sigea-app</artifactId>
<groupId>ar.edu.unt.sigea</groupId>
<version>1.0</version>
</parent>
<groupId>ar.edu.unt.sigea</groupId>
<artifactId>sigea-web</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>sigea-web</name>
<dependencies>
<!-- Some dependencies including sigea-model -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.xml[sigea-model] is not important as it just defines some dependencies for test and is configured to generate a package with the test classes, which are used in sigea-web for test purposes also.
Finally the question: What's failing in my configuration that doesn't package the persistence.xml file? If that's not the problem for the IllegalStateException with the message shown above: What are posible causes for that exception?
Thanks in advance for your answers.
I solved the problem by changing a dependency. In sigea-model I had
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
<scope>test</scope>
</dependency>
Which I used to manage the persistence context for my test methods. I didnt inquire very much in the Glassfish JPA provided implementation but maybe it's Eclipse Link instead of Hibernate. Apparently there are some incompatibility issue between those libraries. I movedhibernate-entitymanager` to compile scope, like this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
And now the projects compile with no problem. It was also useful the comment made by khmarbaise, it simplified the project configuration, thank you very much.

How to disable karaf-maven-plugin 4 tight dependency constraint checks

Currently I am moving from karaf 3.0.5 to the newest version 4.0.2, I do assembly my own karaf with the karaf-maven-plugin. This is how my pom looks like.
<?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">
<parent>
<groupId>my.own.group</groupId>
<artifactId>assemble</artifactId>
<version>1.14.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>karaf-customize</artifactId>
<modelVersion>4.0.0</modelVersion>
<packaging>karaf-assembly</packaging>
<dependencies>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>framework</artifactId>
<version>${karaf.version}</version>
<type>kar</type>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>standard</artifactId>
<classifier>features</classifier>
<version>${karaf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf.karaf</groupId>
<artifactId>apache-cxf</artifactId>
<classifier>features</classifier>
<version>${cxf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>enterprise</artifactId>
<classifier>features</classifier>
<version>${karaf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>my.own.group</groupId>
<artifactId>kar-archive</artifactId>
<version>1.14.0-SNAPSHOT</version>
<type>pom</type>
<optional>true</optional>
</dependency>
<dependency>
<groupId>my.own.group</groupId>
<artifactId>karaf-branding</artifactId>
<version>1.14.0-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.alutam</groupId>
<artifactId>ziputils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>${karaf.version}</version>
<extensions>true</extensions>
<configuration>
<javase>1.8</javase>
<bootFeatures>
<feature>jasypt-encryption</feature>
<feature>config</feature>
<feature>standard</feature>
<feature>region</feature>
<feature>management</feature>
<feature>bundle</feature>
<feature>package</feature>
<feature>kar</feature>
<feature>ssh</feature>
<feature>http</feature>
<feature>cxf</feature>
<feature>service-wrapper</feature>
<feature>jdbc</feature>
<feature>system</feature>
</bootFeatures>
</configuration>
</plugin>
</plugins>
</build>
</project>
With this configuration I do get the following error for several dependencies.
Caused by: org.osgi.framework.BundleException: Unsupported 'Bundle-ManifestVersion' value: 1
at org.apache.karaf.features.internal.resolver.ResourceBuilder.doBuild(ResourceBuilder.java:88)
at org.apache.karaf.features.internal.resolver.ResourceBuilder.build(ResourceBuilder.java:78)
I guess it happens within this parser. The reason is some old third party libraries have only Bundle-ManifestVersion: 1 set within their manifest file.
With karaf-maven-plugin 3.x this didn't matter at all. In contrast the karaf-maven-plugin 4.x fails with message above.
The only way I know to fix this is either rebuild from source or repack the hole jar again.
Is there any other way like a configuration for the karaf-maven-plugin to disable this constraint check? Because it would be awful lot of work to get all of this bundles up an running, again.
I faced the same error when updating to Karaf 4 and you have two choices:
Osgify conflictive dependency using bndtools:
Download bnd tools
Open a shell where you have downloaded bnd-2.4.0.jar.
Type:
java -jar bnd-2.4.0.jar wrap -o osgify-dependency.jar dependency.jar
where dependency.jar is your third party and osgify-dependency.jar will be the output.
Deploy to maven repo overriding the previous maven coordinates, or deploy your thirdparty with different coordinates.
mvn deploy:deploy-file -Dfile osgify-dependency.jar ..
Enable the wrap protocol
Add to you maven karaf plugin wrap and wrapper features.
So you can use wrap protocol to fix your corrupted MANIFEST.MF
Inside some karaf features:
<bundle>wrap:mvn:group.id/third.party.artefact.id/version</bundle>
Inside your pom.xml notice feature wrap / wrapper.
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
</executions>
<configuration>
<!-- no startupFeatures -->
<bootFeatures>
<feature>feature</feature>
<feature>jaas</feature>
<feature>shell</feature>
<feature>ssh</feature>
<feature>management</feature>
<feature>bundle</feature>
<feature>config</feature>
<feature>deployer</feature>
<feature>diagnostic</feature>
<feature>instance</feature>
<feature>kar</feature>
<feature>log</feature>
<feature>package</feature>
<feature>service</feature>
<feature>system</feature>
<feature>wrap</feature>
<feature>aries-blueprint</feature>
</bootFeatures>
<installedFeatures>
..
<feature>wrapper</feature>
</installedFeatures>
</configuration>
</plugin>
Here you have the full code where i tested:
https://github.com/antoniomaria/gazpachoquest/blob/master/karaf-assembly/pom.xml

Apache Felix OSGI installing dependencies

Following on from OSGI bundle dependencies
I have reverted maven-bundle-plugin back to using the defaults. Here is my current pom:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.felix.test</groupId>
<artifactId>com.felix.test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Everything bundles and installs OK. When I try to start the bundle I'm told I'm missing net.sf.ehcache which I install. Then I'm missing slf4j.api which I install. Then I'm missing slf4j.impl and I have tried installing pretty much every slf4j.impl possibility from https://jpm4j.org/#!/ but most (slf4j-simple-1.7.12.jar, slf4j-log4j12-1.7.12.jar) report back:
org.osgi.framework.BundleException: Fragment bundles can not be
started.
This is my current error from GoGo:
org.osgi.framework.BundleException: Unable to resolve com.felix.test
[16](R 16.0): missing requirement [com.felix.test [16](R 16.0)]
osgi.wiring.package;
(&(osgi.wiring.package=net.sf.ehcache)(version>=2.10.0)(!(version>=3.0.0)))
[caused by: Unable to resolve net.sf.ehcache [17](R 17.0): missing
requirement [net.sf.ehcache [17](R 17.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j)(version>=1.7.0)(!(version>=2.0.0)))
[caused by: Unable to resolve slf4j.api [23](R 23.0): missing
requirement [slf4j.api [23](R 23.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]] Unresolved
requirements: [[com.felix.test [16](R 16.0)] osgi.wiring.package;
(&(osgi.wiring.package=net.sf.ehcache)(version>=2.10.0)(!(version>=3.0.0)))]
Hopefully I'm getting closer...
Thank you!
You have two errors to address. The first is "fragment bundles cannot be started". The error message tells you everything you need to know. The slf4j implementation bundles are fragments, and you cannot start fragments. So just don't start them!
You haven't specified how you are running your OSGi Framework, but somewhere you must have some code that iterates over all the installed bundles and calls the start() method on each. You need to modify your code to not call start() on bundles that are fragments. You can tell if any bundle is a fragment as follows:
(bundle.adapt(BundleRevision.class).getTypes() | BundleRevision.TYPE_FRAGMENT) > 0;
OR:
bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;
The second error says that the bundle named com.felix.test has a dependency on package net.sf.ehcache. I have never heard of com.felix.test... is this the bundle you are building? Do you actually use Ehcache in your code? If so, you obviously need to install the Ehcache bundle. If you do have Ehcache installed, then it might be the wrong version; your bundle requires version 2.10.0 up to but excluding 3.0.0.

OSGI org.slf4j.impl dependency

I'm new to OSGI (sorry) and having a few issues trying to deploy my package and related dependencies.
This is my POM:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.felix.test</groupId>
<artifactId>com.felix.test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
com.felix.test.search
</Export-Package>
<Bundle-SymbolicName>
${project.artifactId}
</Bundle-SymbolicName>
<Bundle-Activator>
com.felix.test.Activator
</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I'm bundling this using the Maven command:
mvn org.apache.felix:maven-bundle-plugin:bundleall
This is successful and generates my bundle as well as 3 dependency bundles:
net.sf.ehcache_2.10.0.jar
org.apache.commons.lang3_3.4.0.jar
slf4j.api_1.7.7.jar
This seems OK and I can install and start the first two but when I try and start slf4j I get the following exception:
org.osgi.framework.BundleException: Unable to resolve slf4j.api [25](R
25.0): missing requirement [slf4j.api [25](R 25.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0)) Unresolved
requirements: [[slf4j.api [25](R 25.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]
I'm pretty sure I'm missing something very simple but can't pin it down. Any help would be much appreciated!
Slf4j has an unusual design (some might say a bad design, ahem). It is an API bundle that depends on an implementation package, namely org.slf4j.impl.
You need to install an additional bundle that implements the Slf4j API. There are lots of choices here... for example slf4j-simple is a basic implementation, whereas slf4j-jdk14 uses the Java 1.4 java.util.logging back end, etc.
Logback also contains an implementation of the API.
Need to correct myself as slf4j indeed provides bundles now as Neil pointed out. Not sure how well it works though. I found some explanation how to install slf4j for OSGi here. It does not look very clean to me though. You need to create a bundle fragment for the configuration. Which means you can not change it at runtime.
So I still would rather recommend to use pax-logging at runtime instead. It implements the slf4j api as well as other logging APIs. As backend it uses log4j and configures it via config admin. So you do not need hacks for the logging config and can change it at runtime.
I had this same error message, and found out that it was due to bad bundle plugin configuration (as mentioned by #Christian_Schneider).
My situation
So i had roughly this error message when deploying on Karaf:
missing requirement ... Unresolved requirements: ... osgi.wiring.package=org.slf4j.impl
My modules pom.xml looked like this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j_version}</version>
<scope>provided</scope>
</dependency>
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Private-Package>*</Private-Package>
</instructions>
</configuration>
</plugin>
Solution
credits #jbonofre from the karaf community
I just repaced <Private-Package>*</Private-Package> with <Export-Package></Export-Package>, to get:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Export-Package></Export-Package>
</instructions>
</configuration>
</plugin>
and consequently the import of org.slf4j.impl in my bundles manifest.mf disappeared, leaving only an import to org.slf4j. This is provided by PAX Logging, which is installed by default on karaf.

Categories

Resources