1

How to configure dagger to inject groovy classes, and to inject into groovy classes?

I was initially trying to get dagger to inject a groovy class into my java app, and I found dagger was complaining the groovy class is not found. Looking at the log, it seems that compileGroovy happens after compileJava. And the annotation processing of dagger compiler seems to be in compileJava. I guessed that might be the problem -- no groovy classes are available at this time. But I've yet figured out a way to coerce either of dagger or groovy to work with the other.

It seems I could not upload a .tar.gz. But if anyone needs a minimal demo code for what I meant to achieve, these might help (with gradle 7):

build.gradle:

plugins {
  id 'groovy'
  id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
  mavenCentral()
}

dependencies {
  annotationProcessor 'com.google.dagger:dagger-compiler:2.+'
  implementation 'com.google.dagger:dagger:2.+'
  implementation 'org.codehaus.groovy:groovy-all:3.+'
}

settings.gradle:

rootProject.name = 'groovy-dagger1'

src/main/groovy/org/example/dagger/MainComponent.groovy:

package org.example.dagger

import dagger.Component

@Component(modules = [
    MainModule,
])
interface MainComponent {
  String message();
}

src/main/groovy/org/example/dagger/MainModule.groovy:

package org.example.dagger

import dagger.Module
import dagger.Provides

@Module
final class MainModule {
  @Provides
  static String message() {
    return 'Hello Groovy Dagger!'
  }
}

src/main/groovy/org/example/main/Main.groovy:

package org.example.main;

class Main {
  static void main(String[] args) {
// Dagger component does not exist :/
// println DaggerMainComponent.create().message()
  }
}
Vin
  • 559
  • 2
  • 18

1 Answers1

1

By default, the groovy compiler will not run the java annotation processors...

You can add this to your build.gradle:

compileGroovy {
    groovyOptions.javaAnnotationProcessing = true
}

You will of course need to add an import

import org.example.dagger.DaggerMainComponent

To Main.groovy

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks @tim_yates for the answer. However, point 1 is not working for me. Do help to put details for this if I'd need extra setup to explicitly enable the "joint compilation". Nevertheless, point 2 is giving me some leads and progress. That option does allow a pure groovy project to compile and run correctly with dagger! However it seems for the mixed java and groovy code to compile and run, all source code, both java and groovy, must be in `main/groovy` folder. I tested on gradle 7 only so far. Not sure if the behavior would be different in other versions. – Vin Jul 16 '21 at 06:59
  • (This should be another question, but just FTR) IntelliJ (which I use) does not seem to be able to pick up the annotation processor's generated code automatically, when the code is under `main/groovy`, even when gradle could compile and run it correctly. Manual setup can be applied but would be wiped when reloading the project. – Vin Jul 16 '21 at 07:01
  • [Gradle's groovy plugin guide](https://docs.gradle.org/current/userguide/groovy_plugin.html) mentioned that groovy code in `main/java` would not be compiled. Only java code in `main/groovy` would be. – Vin Jul 16 '21 at 07:27
  • "_main/java would not be compiled. Only java code in main/groovy would be._" Gah, sorry, you're right :-( – tim_yates Jul 16 '21 at 08:03