【问题标题】:How to exclude version numbers from Import-Package with maven-bundle-plugin?如何使用 maven-bundle-plugin 从 Import-Package 中排除版本号?
【发布时间】:2016-07-11 09:06:16
【问题描述】:

我在使用 maven-bundle-plugin 生成的 MANIFEST.MF 时遇到问题。出于某种原因,当我在 <Import-Package> 字段中列出了版本号时,OSGi 框架不会加载我的包。

我进行了实验并注意到,如果我删除清单中的版本号,那么捆绑包就会正确加载。

如何指示 maven-bundle-plugin 跳过版本号?

目前,它生成:

Import-Package: com.ghc.ghTester.expressions,org.apache.ws.security.proc
 essor;version="[1.5,2)",org.apache.ws.security;version="[1.5,2)",org.ap
 ache.ws.security.message;version="[1.5,2)",org.apache.ws.security.compo
 nents.crypto;version="[1.5,2)",org.apache.ws.security.message.token;ver
 sion="[1.5,2)"

但我需要它来生成:

导入包:com.ghc.ghTester.expressions,org.apache.ws.security.proc essor,org.apache.ws.security,org.apache.ws.security.message,org.apache。 ws.security.components.crypto,org.apache.ws.security.message.token

我的插件配置是:

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>3.0.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${pom.groupId}.${pom.artifactId};singleton:=true</Bundle-SymbolicName>
                    <Bundle-Name>${pom.name}</Bundle-Name>
                    <Bundle-Version>${pom.version}</Bundle-Version>
                    <Bundle-ClassPath>{maven-dependencies},.</Bundle-ClassPath>
                    <Embed-Dependency>*;scope=compile</Embed-Dependency>
                    <Export-Package/> <!-- nothing for this bundle to export -->
                    <Import-Package>com.ghc.ghTester.expressions,org.apache.ws.*</Import-Package>
                </instructions>
            </configuration>
        </plugin>

如果我尝试使用版本加载它,我会收到以下错误:

org.osgi.framework.BundleException: Could not resolve module: com.rit.message-level-security [978]
  Unresolved requirement: Import-Package: org.apache.ws.security; version="[1.0.0,3.0.0)"

        at org.eclipse.osgi.container.Module.start(Module.java:434)
        at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:393)
        at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:412)
        at com.ghc.ghTester.Activator.installTempBundle(Activator.java:157)

【问题讨论】:

  • 省略版本不是一个好习惯。部署捆绑包时遇到什么错误?你不能使用你的依赖部署的版本?
  • 我意识到这不是一个好的做法,但这是一个非常有限的插件,将在非常有限的框架中使用。该框架完全不受我的控制,当我尝试加载我的包时,我什至看不到错误消息。通过反复试验,我注意到导致问题的是版本号,最简单的规避方法是删除版本。
  • 如果您使用的是 felix,您可以在 gogo shell 上使用exports 命令来查找导出的包的版本?我认为用这个插件省略版本是不可能的。也许像version=[0,9)这样的东西可以工作,但它真的很难看,违背了osgi依赖管理的目的
  • 我终于设法注销了一些,并粘贴了上面的错误。基本上,找不到版本。
  • 您可以定义:packageName;version="0"。这意味着所有版本都被接受,就好像没有指定范围一样,这意味着大于或等于指定的版本。并使用前面一个 cmets 中提到的双重代码。

标签: maven eclipse-plugin osgi apache-felix


【解决方案1】:

您可以使用以下配置禁用Import-Package 版本:

<_consumer-policy>$${range;[--,++)}</_consumer-policy>

【讨论】:

    【解决方案2】:

    Import-Package 部分中将version=! 添加到您要从中省略版本的每个捆绑包中就可以了。

    <Import-Package>
        com.ghc.ghTester.expressions;version=!,
        org.apache.ws.security.processor;version=!,
        org.apache.ws.security;version=!,
        org.apache.ws.security.message;version=!,
        org.apache.ws.security.components.crypto;version=!,
        org.apache.ws.security.message.token;version=!,
        *
    </Import-Package>
    

    【讨论】:

      【解决方案3】:

      您可以像使用“com.ghc.ghTester.expressions”一样开始手动编写导入

      <Import-Package> 
      com.ghc.ghTester.expressions,
      org.apache.ws.security.processor,
      org.apache.ws.security,
      org.apache.ws.security.message,
      org.apache.ws.security.components.crypto,
      org.apache.ws.security.message.token
      </Import-Package>
      

      尽管这不是 cmets 中提到的好习惯,但它应该可以解决问题。但是,如果您以后需要额外的导入,您也必须手动添加它们。

      顺便一提。

      的值
      <Bundle-Name>${pom.name}</Bundle-Name>
      <Bundle-Version>${pom.version}</Bundle-Version>
      

      默认为您提供的值。见http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html 部分:默认行为

      【讨论】:

        猜你喜欢
        • 2012-09-08
        • 2013-09-01
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多