Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Dagger dependency injection #110

Open
drigz opened this issue Jan 30, 2018 · 2 comments
Open

Add support for Dagger dependency injection #110

drigz opened this issue Jan 30, 2018 · 2 comments

Comments

@drigz
Copy link
Contributor

drigz commented Jan 30, 2018

I tried to use the annotation processor support from #93 to use Dagger but I got the error java.lang.ClassNotFoundException: com.google.common.collect.Multimap from the annotation processor when compiling.

My config:

  com.google.dagger:
    dagger-compiler:
      lang: java
      version: "2.11"
      processorClasses: ["dagger.internal.codegen.ComponentProcessor"]

This generates the following rules in java/com/google/dagger/BUILD: (adjusted for brevity)

java_library(
    name = "dagger",
    exports = ["//external:jar/com/google/dagger/dagger"],
)

java_library(
    name = "dagger_compiler",
    exported_plugins = [":dagger_compiler_plugin"],
    exports = ["//external:jar/com/google/dagger/dagger_compiler"],
    runtime_deps = [
        ":dagger",
        ":dagger_producers",
        "//third_party/java/com/google/code/findbugs:jsr305",
        "//third_party/java/com/google/googlejavaformat:google_java_format",
        "//third_party/java/com/google/guava",
        "//third_party/java/com/squareup:javapoet",
        "//third_party/java/javax/inject:javax_inject",
    ],
)

java_plugin(
    name = "dagger_compiler_plugin",
    processor_class = "dagger.internal.codegen.ComponentProcessor",
    deps = ["//external:jar/com/google/dagger/dagger_compiler"],
)

However, this doesn't work as the annotation processor depends on the dagger compiler, which in turn has dependencies. In other words, :dagger_compiler_plugin should depend on :dagger_compiler, not //external:jar/com/google/dagger/dagger_compiler. It's also missing generates_api = True. The correct rules are:

java_library(
    name = "dagger",
    exports = ["//external:jar/com/google/dagger/dagger"],
)

java_library(
    name = "dagger_compiler",
    exports = ["//external:jar/com/google/dagger/dagger_compiler"],
    runtime_deps = [
        ":dagger",
        ":dagger_producers",
        "//third_party/java/com/google/code/findbugs:jsr305",
        "//third_party/java/com/google/googlejavaformat:google_java_format",
        "//third_party/java/com/google/guava",
        "//third_party/java/com/squareup:javapoet",
        "//third_party/java/javax/inject:javax_inject",
    ],
)

java_plugin(
    name = "dagger_compiler_plugin",
    generates_api = True,
    processor_class = "dagger.internal.codegen.ComponentProcessor",
    deps = [":dagger_compiler"],
)

java_library(
    name = "dagger_with_compiler",
    exported_plugins = [":dagger_compiler_plugin"],
    exports = [":dagger"],
)

Workaround

  • Remove the processorClasses setting from dependencies.yaml to avoid the incorrect plugin definition.
  • Add these build rules, eg to //src/java:
java_plugin(
    name = "dagger_compiler_plugin",
    generates_api = True,
    processor_class = "dagger.internal.codegen.ComponentProcessor",
    deps = ["//third_party/java/com/google/dagger:dagger_compiler"],
)

java_library(
    name = "dagger_with_compiler",
    exported_plugins = [":dagger_compiler_plugin"],
    exports = ["//third_party/java/com/google/dagger"],
)
  • Depend on :dagger_with_compiler where needed.

It's probably also possible to reuse the Bazel BUILD files from the Dagger repository, but I haven't tried this yet.

@hsyed
Copy link
Contributor

hsyed commented Feb 15, 2018

neverlink and testonly also need to be added

@johnynek
Copy link
Collaborator

happy to accept a PR to fix this up.

Sorry it isn't working currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants