Search code examples
mavenosgi-bundle

How to manage import and export packages when manifest.mf is autogenerated


I am working on a project where the MANIFEST.MF is autogenerated. When I deploy my OSGi bundles in the Felix Console, I can see dependency issues

> 09.02.2017 08:15:16.258 *ERROR* [FelixDispatchQueue] xxx.core FrameworkEvent ERROR (org.osgi.framework.BundleException: Unresolved
> constraint in bundle xxx.core [446]: Unable to resolve 446.1: missing
> requirement [446.1] osgi.wiring.package;
> (&(osgi.wiring.package=xxx.acs.commons.dispatcher)(version>=1.0.0)(!(version>=2.0.0))))
> org.osgi.framework.BundleException: Unresolved constraint in bundle
> xxx.core [446]: Unable to resolve 446.1: missing requirement [446.1]
> osgi.wiring.package;
> (&(osgi.wiring.package=xxx.acs.commons.dispatcher)(version>=1.0.0)(!(version>=2.0.0)))
>   at
> org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4095)
>   at org.apache.felix.framework.Felix.startBundle(Felix.java:2114)    at
> org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1368)
>   at
> org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:308)
>   at java.lang.Thread.run(Thread.java:745)

All solutions around such issues say making changes in the Import and Export packages, but in project, the manifest is getting auto-generated.

Any help in how to fix this issue will be appreciated.


Solution

  • For maven project the maven-bundle-plugin can be used.

    For other, standard java library use BND Tools which is used by maven-bundle plugin as api too.

    Example of a pom.xml which repackage apache-poi and adding the required manifest entries:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <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>
        <version>3.13.1</version>
    
        <properties>
            <poi.version>3.13</poi.version>
            <poi.schema.version>1.1</poi.schema.version>
            <poi.security.version>1.0</poi.security.version>
        </properties>
    
        <groupId>your.group.id</groupId>
        <artifactId>external-apache-poi</artifactId>
        <packaging>bundle</packaging>
        <name>external-apache-poi</name>
        <description>Apache poi framework</description>
    
        <build>
        <plugins>
    
            <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.5.3</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                            <_exportcontents>
                                org.apache.poi.*;version=${poi.version},
                                org.openxmlformats.schemas.*;version=${poi.schema.version},
                                schemasMicrosoftComOfficeExcel.*;version=${poi.schema.version},
                                schemasMicrosoftComOfficeOffice.*;version=${poi.schema.version},
                                schemasMicrosoftComOfficePowerpoint.*;version=${poi.schema.version},
                                schemasMicrosoftComVml.*;version=${poi.schema.version},
                                org.etsi.uri.*;version=${poi.security.version}
                            </_exportcontents>
                            <Import-Package>
                                com.sun.javadoc;resolution:=optional,
                                com.sun.tools.javadoc;resolution:=optional,
                                org.apache.crimson.jaxp;resolution:=optional,
                                org.apache.tools.ant;resolution:=optional,
                                org.apache.tools.ant.taskdefs;resolution:=optional,
                                org.apache.tools.ant.types;resolution:=optional,
                                junit.framework.*;resolution:=optional,
                                junit.textui.*;resolution:=optional,
                                org.junit.*;resolution:=optional,
                                org.apache.xml.security.*;resolution:=optional,
                                org.apache.jcp.xml.dsig.internal.dom.*;resolution:=optional,
                                *
                            </Import-Package>
                            <DynamicImport-Package>
                                org.apache.xmlbeans.*,
                                schemaorg_apache_xmlbeans.*
                            </DynamicImport-Package>
    
                <!-- bundle supplied resource prefixes -->
                <Include-Resource>{maven-resources}</Include-Resource>
    
                <!-- Do not inline jars, include as jar files -->
                <!-- There are config files with same name will be overwritten -->
                <Embed-Dependency>*;scope=compile;inline=false</Embed-Dependency>
    
    
                </instructions>
            </configuration>
            </plugin>
        </plugins>
        </build>
        <dependencies>
            <!-- Embedded dependencies -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${poi.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>ooxml-schemas</artifactId>
                <version>${poi.schema.version}</version>
            </dependency>
            <dependency>
               <groupId>org.apache.poi</groupId>
               <artifactId>ooxml-security</artifactId>
               <version>${poi.security.version}</version>
            </dependency>
        </dependencies>
    
    </project>