OSGiWrapper

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 63: Line 63:
<source lang="bash">
<source lang="bash">
-
$ mvn install
+
$ mvn package
$ unzip -l target/jar-id-1.0-SNAPSHOT.jar
$ unzip -l target/jar-id-1.0-SNAPSHOT.jar
# shortened ...
# shortened ...

Revision as of 13:24, 29 November 2014

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 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. Copy the above file into pom.xml and execute:

$ 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

Wrap your JAR's into OSGi bundles and let Bck2Brwsr obfuscate them per library.

Personal tools
buy