如何为运行时依赖关系添加Import-Package指令? [英] How to add Import-Package instructions for runtime dependencies?

查看:375
本文介绍了如何为运行时依赖关系添加Import-Package指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gradle构建OSGi jar,并且我在清单中遇到了一些问题。下面是我的构建脚本的摘录:

  apply plugin:'java'
apply plugin:'osgi'

jar {
baseName ='awesome'
manifest {
name ='一个令人敬畏的应用程序'
symbolicName ='com.example.awesome'
//指令'Import-Package','org.springframework.orm'
}
}

使用上面的代码,Gradle检测到我的依赖关系,并添加了一个 Import-Package 和我所有的编译时间依赖项。但是说我的应用程序对 org.springframework.orm 还有一个运行时依赖项,我该如何告诉Gradle将它作为参数包含在 Import- Package



通过上面的注释行取消注释,生成的 Import-Package 指令仅指定 org.springframework.orm 。这让我感到困惑,因为 OsgiManifest的Gradle文档声明


OsgiManifest指令(字符串名称,字符串...值)



将参数添加到指令中。如果该指令不存在,则创建
。如果它存在,参数将被追加到
现有参数中。


这似乎不适用于自动检测到 Import-Package 参数。如果我指定指令'Import-Package','my.dependency.1'等等,并打印 jar.manifest.instructions 之间,我可以看到它们是如何按顺序添加的,并且它们都出现在生成的清单中,但似乎Gradle的OSGi插件不会添加自动检测的参数(如果 Import-手动指定包裹



TL; DR:




  • 如果我没有手动指定 Import-Package 指令,它会自动生成所有我的编译时依赖项,而不是运行时依赖项。

  • 如果我执行 manifest {instruction'Import-Package','org.springframework.orm'} ,那么编译时依赖不会自动



如何告诉Gradle我的运行时依赖关系,以便将它们添加到 Import-Package code>?

解决方案

我不熟悉Gradle bnd p lugin,但我对bnd很熟悉。



在bnd指令中, Import-Package 的默认值是 * 。手段:匹配所有生成的进口。如果为 Import-Package 指定单个值,则它会覆盖 * 并成为唯一导入。因此,您的 Import-Package 指令几乎总是以最后一个位置的全部 * 结束。



猜测这将如何转化为Groovy / Gradle,我建议如下:


指令'Import-Package','org.springframework.orm','*'


I'm building an OSGi jar using Gradle, and I'm having a bit of trouble with the manifest. Here's an excerpt from my build script:

apply plugin: 'java'
apply plugin: 'osgi'

jar {
    baseName = 'awesome'
    manifest {
        name = 'An Awesome Application'
        symbolicName = 'com.example.awesome'
//        instruction 'Import-Package', 'org.springframework.orm'
    }
}

Using the above, Gradle detects my dependencies and adds an Import-Package with all my compile-time dependencies. But say my application has a runtime dependency on org.springframework.orm as well, how do I tell Gradle to include that as an argument in Import-Package?

With the commented row above uncommented, the generated Import-Package instruction specifies only org.springframework.orm. This confuses me, since the Gradle documentation for OsgiManifest states

OsgiManifest instruction(String name, String... values)

Adds arguments to an instruction. If the instruction does not exists, it is created. If it does exists, the arguments are appended to the existing arguments.

This doesn't seem to apply for the auto-detected Import-Package arguments, however. If I specify instruction 'Import-Package', 'my.dependency.1' and so on, and print jar.manifest.instructions in between, I can see how they are added in sequence, and they all appear in the generated manifest, but it seems that Gradle's OSGi plugin does not add the auto-detected arguments if Import-Package is manually specified.

TL;DR:

  • If I do not manually specify the Import-Package instruction, it is auto-generated with all my compile-time dependencies, but not runtime dependencies.
  • If I do manifest { instruction 'Import-Package', 'org.springframework.orm' }, then the compile-time dependencies are not automatically included.

How can I tell Gradle my runtime dependencies so they can be added to Import-Package?

解决方案

I'm not familiar with the Gradle bnd plugin, but I am familiar with bnd.

The default value of Import-Package in bnd instruction is *. The means: match against all of the generated imports. If you specify a single value for Import-Package then it overrides the * and becomes the sole import. Therefore your Import-Package instruction nearly always should end with a catch-all * in the last position.

Taking a guess at how this would translate to Groovy/Gradle, I suggest the following:

instruction 'Import-Package', 'org.springframework.orm', '*'

这篇关于如何为运行时依赖关系添加Import-Package指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆