<Export-Package> for all resources using maven-bundle-plugin

Hilikus picture Hilikus · Jul 17, 2012 · Viewed 20.1k times · Source

As a temporary measure to be able to transition quickly to OSGi I need to create a single jar with all my libraries in it. What i did was put all the jar libraries in src/main/resources so they end up in the root of the created jar. The problem i am having is telling the maven-bundle-plugin to export ALL the packages in the jars. So basically, i want to expose all my libraries to other OSGi bundles

This is the first thing I tried in my POM

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Export-Package>*</Export-Package>
                    <Bundle-Name>${project.artifactId}</Bundle-Name>
                    <Bundle-Version>${project.version}</Bundle-Version>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>`

I tried to export everything there was. But it seems that the only thing that gets exported like this are the two osgi dependencies, not the jars in the resources

I have over a hundred libraries so i am trying to find an automated way to populate the <Export-Package> directive instead of adding each librarie's package by hand. Somehow eclipse does it in the plugin development environment, but i need to do this using maven. Is this possible with the bundle plugin at all? Extra points if the jars get added to the <Bundle-ClassPath>

Answer

cstroe picture cstroe · Nov 22, 2013

You must add the jars as dependencies in your pom.xml and then use the following formulation for your maven-bundle-plugin in the <build> tag:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <manifestLocation>META-INF</manifestLocation>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Bundle-Version>${project.version}</Bundle-Version>
            <Export-Package>*</Export-Package>
            <Bundle-Activator>your.activator.package.Activator</Bundle-Activator>
            <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
            <Embed-Directory>target/dependency</Embed-Directory>
            <Embed-StripGroup>true</Embed-StripGroup>
            <Embed-Transitive>true</Embed-Transitive>
        </instructions>
    </configuration>
</plugin>

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Also add the following to make everything work with m2e:

See: maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e

<pluginManagement>
    <plugins>
        <!-- Ignore/Execute plugin execution -->
    <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <!-- copy-dependency plugin -->
                        <pluginExecution>
                <pluginExecutionFilter>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <versionRange>[1.0.0,)</versionRange>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <ignore />
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

Also add the following to make it work with Eclipse PDE (taken from Apache Felix website):

<profiles>
    <profile>
        <activation>
            <property>
                <name>m2e.version</name>
            </property>
        </activation>
        <properties>
            <osgi-version-qualifier>qualifier</osgi-version-qualifier>
        </properties>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <configuration>
                            <!-- PDE does not honour custom manifest location -->
                            <manifestLocation>META-INF</manifestLocation>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>