Sunday, January 27, 2013

Wrapper bundle for non-OSGi packages

All we need is add Bundle-ClassPath to the wrapper's manifest.mf and packaged it with dependency plugin.

Below is an example of how to build a wrapper for Jedis.

Manifest-Version: 1.0
Bundle-Version: 2.1.0
Bundle-ManifestVersion: 2
Bundle-Name: Jedis Wrapper
Bundle-Description: Wrapper bundle for Jedis
Import-Package: org.slf4j, org.apache.commons.pool.impl, org.apache.commons.pool
Export-Package: redis.clients.jedis;version="2.1.0",redis.clients.util;version="2.1.0",redis.clients.jedis.exceptions;version="2.1.0"
Bundle-SymbolicName: wrapper-jedis_bundle
Bundle-ClassPath: .,jedis-2.1.0.jar

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>redis.clients</groupId>
    <artifactId>wrapper-jedis</artifactId>
    <packaging>jar</packaging>
    <version>2.1.0</version>
    <name>wrapper-jedis</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeArtifactIds>jedis</includeArtifactIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.1.0</version>
    </dependency>
    <dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.5</version>
</dependency>
    </dependencies>
</project>

Granted, the package exported or imported is a bit trail-and-error.  don't know if there's any good tools that can look into the bytecode of a jar file and generate the manifest file.  maybe bnd can, but haven't try that.