Converting simple jars into OSGI bundles

If for some reason you are working with OSGI, at some point you’ll probably need to use 3rd party dependencies to complete your job, unfortunately, not all of those dependencies are built with OSGI in mind.

Sometimes it’s easy some aren’t, for example, when the package creator ignores good practices and creates classes with no packages (in the root of the project/package), then we have to deal with this before converting the package into OSGI.

1# Converting into OSGI

For this task, we can benefit of the maven-bundle-plugin that set the necessary properties into META-INF/MANIFEST.MF file, making it OSGI compatible.

2# Avoiding the root classes problem or “The default package ‘.’ is not permitted by the Import-Package syntax”

To do that and skip classes without any package, you can use the instructions section to export just the packages you need to complete your job.

Following you can see a sample of how to fix those 2 problems, I hope it can be useful to you 😉

<?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>java4.fun.osgi</groupId>
   <artifactId>ig-osgi-imagej</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <dependencies>
      <dependency>
         <groupId>net.imagej</groupId>
         <artifactId>ij</artifactId>
         <version>1.51h</version>
         <optional>false</optional>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
               <archive>
                  <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
               </archive>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
               <unpackBundle>true</unpackBundle>
               <instructions>
                  <Export-Package>
                     ij,
                     ij.process
                  </Export-Package>

                     *;scope=compile|runtime;inline=true
                  </Embed-Dependency>
                  <Import-Package>
                     *
                  </Import-Package>
               </instructions>
            </configuration>
            <executions>
               <execution>
                  <id>bundle-manifest</id>
                  <phase>process-classes</phase>
                  <goals>
                     <goal>manifest</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Allan de Queiroz

Allan de Queiroz
London based software engineer

XServer forward from Linux text mode for Headless purposes.

Hello, this post is about XServer forward from Linux text mode, **not ssh forward, anything related to VNC** or things like that.Recently...… Continue reading