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

こ雲淡風輕ζ 提交于 2020-01-02 05:36:28

问题


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>


回答1:


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>



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/11531152/export-package-for-all-resources-using-maven-bundle-plugin

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