How to embed third party jars in OSGI bundle

If you are writing code which is using some third party jars, you would like to embed them in the final bundle.

So here is a sample pom.xml which helps in embedding. My code was using a third party library : restfb. In dependency I have specified that.

Now to include it in the final bundle we need to make use of <embed-dependency>. Here I have specified that as *;scope=compile. So it will embed all the dependencies whose scope was specified as compile which is a default scope.

restfb’s scope is compile so it will be embedded in the bundle. org.osgi.core’s scope is provided, so it will not be embedded. We should not even embed osgi jars as it will be provided by the platform itself. If we embed them, you might start getting classcastexception as Activator class of OSGI might get loaded from platform as well as your embedded jar.

In above example we had embedded osgi core jar also. So Activator class will extend the BundleActivator class of this jar.

Now if use felix container to start this bundle, it will also have osgi core jar. It will expect Activator class to extend the BundleActivator class of this jar but in actual it will be of type BundleActivator present in osgi core jar inside the bundle. Hence you will get classcastexception.

<?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">

	<!-- Licensed to the Apache Software Foundation (ASF) under one or more 
		contributor license agreements. See the NOTICE file distributed with this 
		work for additional information regarding copyright ownership. The ASF licenses 
		this file to you under the Apache License, Version 2.0 (the "License"); you 
		may not use this file except in compliance with the License. You may obtain 
		a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
		required by applicable law or agreed to in writing, software distributed 
		under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
		OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
		the specific language governing permissions and limitations under the License. -->

	<modelVersion>4.0.0</modelVersion>

	<groupId>org.ogra</groupId>
	<artifactId>Bundle1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>bundle</packaging>

	<name>simpleBundleTest Bundle</name>
	<description>
        simpleBundleTest OSGi bundle project.
    </description>

	<properties>
		<maven-bundle-plugin.version>2.5.4</maven-bundle-plugin.version>
		<osgi.version>6.0.0</osgi.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.core</artifactId>
			<version>${osgi.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.restfb/restfb -->
		<dependency>
			<groupId>com.restfb</groupId>
			<artifactId>restfb</artifactId>
			<version>2.21.0</version>
		</dependency>

		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.compendium</artifactId>
			<version>4.2.0</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>${maven-bundle-plugin.version}</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>${project.version}</Bundle-Version>
						<Bundle-Activator>org.ogra.bundle1.Activator</Bundle-Activator>
						<Export-Package>
							org.ogra.bundle1*
						</Export-Package>
						<Import-Package>
							*;resolution:=optional
						</Import-Package>
						<Embed-Dependency>*;scope=compile</Embed-Dependency>
						<Embed-Transitive>true</Embed-Transitive>
					</instructions>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *