How to add Import-Package instructions for runtime dependencies?
Asked Answered
C

1

7

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?

Chaulmoogra answered 25/6, 2013 at 13:10 Comment(0)
P
6

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

Pulse answered 25/6, 2013 at 15:1 Comment(1)
Spot on! As expected from the documentation, instruction 'Import-Package', '*' on its own row also works.Chaulmoogra

© 2022 - 2024 — McMap. All rights reserved.