【问题标题】:<Export-Package> for all resources using maven-bundle-plugin<Export-Package> 用于使用 maven-bundle-plugin 的所有资源
【发布时间】:2012-07-16 21:14:43
【问题描述】:

作为能够快速过渡到 OSGi 的临时措施,我需要创建一个包含我所有库的单个 jar。我所做的是将所有 jar 库放在 src/main/resources 中,以便它们最终位于创建的 jar 的根目录中。我遇到的问题是告诉 maven-bundle-plugin 导出 jar 中的所有包。所以基本上,我想将我所有的库暴露给其他 OSGi 包

这是我在 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 依赖项,而不是资源中的 jars

我有一百多个库,所以我试图找到一种自动填充&lt;Export-Package&gt; 指令的方法,而不是手动添加每个库的包。不知何故,eclipse在插件开发环境中做到了,但我需要使用maven来做到这一点。捆绑插件可以做到这一点吗?如果罐子被添加到&lt;Bundle-ClassPath&gt; 中,则加分

【问题讨论】:

    标签: maven osgi maven-bundle-plugin


    【解决方案1】:

    我认为这是不可能的。 jar 需要单独位于 maven 存储库中,以便能够通过将它们作为依赖项添加到库项目中来创建“库项目”;否则罐子不会在类路径中。这样做的一个很好的参考是this page

    【讨论】:

    • 链接失效。如果可以,请更改它。
    【解决方案2】:

    您必须在 pom.xml 中添加 jar 作为依赖项,然后在 标记中为您的 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 (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>
    

    还添加以下内容以使其与 Eclipse PDE 一起使用(取自 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>
    

    【讨论】:

      【解决方案3】:

      根据 bundle 插件的documentation,您可以使用{local-packages},这将扩展到项目中的所有包。

      但是这是一个非常糟糕的主意! 想一想,您是说捆绑包中的所有内容都应该是公开可见的 API。这意味着你必须维护所有这些包,确保你仔细地改进它们并使用正确的版本等。基本上你不是模块化的。

      任何 OSGi 包的理想状态应该是导出尽可能少的包

      【讨论】:

      • 是的,我知道这是个坏主意。这些库最终将被导入 maven repo 并单独管理,但在过渡期间,我需要将所有内容打包到一个库包中。将所有这些库导入 maven 需要时间
      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 2015-05-19
      • 2016-07-11
      • 2013-08-19
      • 2019-08-11
      • 2019-06-25
      • 2012-09-08
      • 2018-02-18
      相关资源
      最近更新 更多