简体   繁体   中英

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
     }

  • I don't get an error when I remove the 3rd referenced service (have only the first two)
  • I do get the error when I reference the 3rd component eg at first or second option.
  • The tasks compileJava work well - apparently no syntax or unresolved import issue
  • I ran gradle with clean --refresh-dependencies , nothing worked
  • I cleared the ~/.gradle/caches directory (Windows home path), without effect
  • This is a "doesn't work on my machine" problem: It runs with "exactly" the same setup (same source, same version of references, same java and gradle version) on other machines.

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

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 :-(

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM