使用maven-bundle-plugin导出所有资源的<Export-Package>

4
作为一个临时措施,为了能够快速过渡到OSGi,我需要创建一个包含所有库的单个JAR文件。我所做的是将所有的JAR库放在“src/main/resources”中,这样它们就会出现在创建的JAR的根目录中。我的问题是如何告诉Maven-Bundle-Plugin导出JAR中的ALL packages。所以基本上,我希望向其他OSGi bundles公开所有我的库。
这是我在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>`

我尝试导出所有内容,但似乎只有两个OSGI依赖项被导出,而不是资源中的JAR包。
我有一百多个库,因此我正在寻找一种自动填充<Export-Package>指令的方法,而不是手动添加每个库的包。某种程度上,Eclipse在插件开发环境中完成了这项工作,但我需要使用Maven来完成。使用Bundle插件是否可以实现这一点?如果JAR包被添加到<Bundle-ClassPath>中,则额外得分。
3个回答

7

您需要在pom.xml文件中添加jar包依赖,并在<build>标签中使用以下公式来配置maven-bundle-plugin:

<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>

此外,为了使所有内容与m2e一起正常工作,请添加以下内容:
请参见:maven-dependency-plugin(目标“copy-dependencies”,“unpack”)不受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>

还需要添加以下内容,使其能够与Eclipse PDE一起使用(从Apache Felix网站中获取):

<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>

4
根据bundle插件的文档,您可以使用{local-packages},这将扩展到项目中的所有包。 但是这是一个非常糟糕的想法!想一想,您正在说您的bundle中的所有内容都应该是公开可见的API。这意味着您必须维护所有这些包,确保仔细演变并具有正确的版本等。基本上,您不是在进行模块化。任何OSGi bundle的理想状态应该是尽可能少地导出包。

是的,我知道这是一个不好的想法。这些库最终将被导入到Maven仓库中并单独管理,但在过渡期间,我需要将所有内容打包成一个单独的库束。将所有这些库导入到Maven需要时间。 - Hilikus

1

我认为这是不可能的。这些jar包需要单独在Maven仓库中才能通过将它们作为依赖项添加到库项目中来创建“库项目”;否则,这些jar包将不会在类路径中。一个很好的参考资料是此页面


链接无法使用,请尽快更换。 - xmlParser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接