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

守給你的承諾、 提交于 2019-12-05 14:00:44
cstroe

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>

According to the documentation for the bundle plugin, you can use {local-packages} and this will be expanded to all of the packages in the project.

HOWEVER this is a really bad idea! Think about it for a second, you're saying that everything in your bundle should be publically visible API. That means you have to maintain all of those packages, make sure you evolve them carefully and with correct versions etc. Basically you're not being modular.

The ideal for any OSGi bundle should be to export as few packages as possible.

I think this is not possible. The jars need to be individually in the maven repo to be able to create a "library project" by adding them as dependencies in the library project; otherwise the jars won't be in the classpath. A good reference for doing this is this page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!