Moritz
Moritz

Reputation: 1125

OSGi: BND tools fail to @Reference only a specifc component

I have an OSGi component which looks like this

    @Activate
    public MyComponent(@Reference OtherServiceA ref1, @Reference OtherServiceB ref2, @Reference OtherServiceC ref3) {
         // remainder omitted
     }

The @Reference annotation does not appear anywhere else in the source.

I'm using gradle 5.6 with the BND tools to build the jar:

// file: build.gradle
plugins {
    id 'biz.aQute.bnd.builder'
}

Running the jar task results in the following error:

> Task :my.pro.ject:jar FAILED
error  : In component my.pro.ject.mypackage.MyComponent, @Reference cannot be used for method parameters

FAILURE: Build failed with an exception.

and the corresponding stack trace

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':my.pro.ject:jar'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$3.accept(ExecuteActionsTaskExecuter.java:166)
[...]
Caused by: org.gradle.api.GradleException: Bundle my.pro.ject-2.0.0-SNAPSHOT.jar has errors
        at aQute.bnd.gradle.BundleTaskConvention.failBuild(BundleTaskConvention.groovy:310)
        at aQute.bnd.gradle.BundleTaskConvention$_buildBundle_closure6$_closure7.doCall(BundleTaskConvention.groovy:294)
        at aQute.bnd.gradle.BundleTaskConvention$_buildBundle_closure6.doCall(BundleTaskConvention.groovy:200)
        at org.gradle.util.ClosureBackedAction.execute(ClosureBackedAction.java:71)
[...]

I believe the error message is misleading: The following code works well:

    @Activate
    public MyComponent(@Reference OtherServiceA ref1, @Reference OtherServiceB ref2) { // just removed ref3
         // remainder omitted
     }

Further details: - I'm using Java 1.8.151(32-bit)

Upvotes: 0

Views: 491

Answers (2)

Arnoud
Arnoud

Reputation: 96

I ran into this same issue, but while using the bnd-maven-plugin (5.2.0) in a project built using a quite ancient Java 8 (1.8.0_60). Updating either to something (more) recent (bnd-maven-plugin to 5.3.0 or java to 1.8.0_162) solved it for me.

I had the debugger attached and noticed the error was produced while processing the @Deactivate annotated method. I did not investigate further why the parameters from the constructor were evaluated as being part of the deactivate method, and why this problem disappears when updating the jdk.

Upvotes: 0

BJ Hargrave
BJ Hargrave

Reputation: 9384

You do not specify the version for the biz.aQute.bnd.builder plugin. The latest release is 5.0.1 which definitely has support for OSGi DS 1.4 annotations which support constructor injection.

You also do not mention which version of the OSGi DS annotations you use. You must use version 1.4 to use constructor injection.

As for why it only fails on your machine, I cannot say :-(

Upvotes: 0

Related Questions