'. '

OSGiWrapper

From APIDesign

Jump to: navigation, search

Not all artifacts in a Maven repository are packaged as OSGi bundles. Yet, some systems (like for example the Bck2Brwsr ahead-of-time compiler) require JAR to contain OSGi meta-data. Luckily there is a simple way to turn any JAR into OSGi bundle by creating a simple Maven project. Here is the sample pom.xml:

<?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>your.group.id</groupId>
    <artifactId>jar-id</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging> <!-- we want to produce an OSGi bundle -->
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <repositories>
      <!-- we are wrapping a JAR from NetBeans Maven repository -->
      <repository>
        <id>netbeans</id>
        <name>NetBeans</name>
        <url>http://bits.netbeans.org/maven2/</url>
      </repository>
    </repositories>
 
    <dependencies>
        <!-- specify the dependency you want to wrap as OSGi bundle -->
        <dependency>
            <groupId>org.netbeans.external</groupId>
            <artifactId>nb-javac-api</artifactId>
            <version>RELEASE80</version>
            <!-- we need the dependency only during compilation -->
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <!-- export the packages that should be externally accessible -->
                        <Export-Package>javax.*</Export-Package>
                        <!-- list other packages that should be included in your bundle -->
                        <Private-Package>com.sun.*</Private-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And that is all! After building the project, you'll get an OSGi bundle that contains all the classes from the specified dependency found in exported and private packages. Just copy the above text into pom.xml file and in its directory execute yourself:

$ mvn package
$ unzip -l target/jar-id-1.0-SNAPSHOT.jar
# shortened ...
     4539  2014-02-14 11:52   com/sun/source/util/Trees.class
      125  2014-02-14 11:52   com/sun/source/util/package-info.class
     5109  2014-02-14 11:53   javax/annotation/processing/AbstractProcessor.class
# ...shortened
$ unzip -p target/jar-id-1.0-SNAPSHOT.jar  META*MAN*
Manifest-Version: 1.0
Bnd-LastModified: 1417267436493
Build-Jdk: 1.8.0_25
Bundle-ManifestVersion: 2
Bundle-Name: jar-id
Bundle-SymbolicName: your.group.id.jar-id
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: javax.annotation.processing;uses:="javax.lang.model,java
 x.tools,javax.lang.model.element,javax.lang.model.util";version="1.0.0.
 SNAPSHOT",javax.lang.model.util;uses:="javax.annotation.processing,java
 x.lang.model,javax.lang.model.element,javax.lang.model.type";version="1
 .0.0.SNAPSHOT",javax.lang.model;uses:="javax.lang.model.element";versio
 n="1.0.0.SNAPSHOT",javax.tools;uses:="javax.lang.model.element,javax.an
 notation.processing,javax.lang.model";version="1.0.0.SNAPSHOT",javax.la
 ng.model.type;uses:="javax.lang.model.element,javax.lang.model";version
 ="1.0.0.SNAPSHOT",javax.lang.model.element;uses:="javax.lang.model.type
 ,javax.lang.model";version="1.0.0.SNAPSHOT"
Import-Package: javax.annotation.processing,javax.lang.model.element,jav
 ax.lang.model.type,javax.lang.model.util,javax.tools
Tool: Bnd-1.50.0

Wrap your JAR's into OSGi bundles and let Bck2Brwsr obfuscate them effectively per library - see the Bck2BrwsrLibraries howto!

Converting Truffle

I've just converted Truffle to OSGi bundle with following simple script. As I was interested only in PolyglotEngine class, I exported just the single package:

<?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>
    <artifactId>truffleosgi</artifactId>
    <groupId>org.apidesign.demo</groupId>
    <packaging>bundle</packaging>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.oracle.truffle</groupId>
            <artifactId>truffle-api</artifactId>
            <version>0.22</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <!-- I am interested only in PolyglotEngine -->
                        <Export-Package>com.oracle.truffle.api.vm</Export-Package>
                        <!-- list other packages that should be included in your bundle -->
                        <Private-Package>META-INF.*,com.*</Private-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Personal tools