How to add Import-Package instructions for runtime dependencies?

跟風遠走 提交于 2019-12-01 02:39:52

问题


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?


回答1:


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', '*'



来源:https://stackoverflow.com/questions/17298263/how-to-add-import-package-instructions-for-runtime-dependencies

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