How to add Import-Package instructions for runtime

2020-03-19 23:34发布

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?

标签: osgi gradle bnd
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-03-20 00:17

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

查看更多
登录 后发表回答