c

    cvmocanu

    4 years ago
    Hello! How are you guys setting up kotlin + dagger2? Is there a way to make IntelliJ automatically run kapt as part of building (Ctrl+F9)? I am trying to use Dagger 2 in a Kotlin / maven project Here is what I tried: (1) setup add dagger-compiler as a dependency. "Settings > Build, Execution, Deployment > Compiler > Annotation Processors" for my maven module has "Enable annotation processing" checked and "Obtain processors from project classpath" So I quickly realized that option (1) obviously won't work if I write my dagger components and modules in kotlin code. So I tried (2) setup the kotlin maven plugin to run kapt with dagger2:
    <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
    
                    <executions>
                        <execution>
                            <id>kapt</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>kapt</goal>
                            </goals>
                            <configuration>
                                <sourceDirs>
                                    <sourceDir>src/main/kotlin</sourceDir>
                                    <sourceDir>src/main/java</sourceDir>
                                </sourceDirs>
                                <annotationProcessorPaths>
                                    <!-- Specify your annotation processors here. -->
                                    <annotationProcessorPath>
                                        <groupId>com.google.dagger</groupId>
                                        <artifactId>dagger-compiler</artifactId>
                                        <version>2.16</version>
                                    </annotationProcessorPath>
                                </annotationProcessorPaths>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    This works from maven, but IntelliJ is completely clueless. By this, I mean that no additional files are generated in my target directory when I press Ctrl+F9 (or choose to rebuild the module). There is only a "classes" directory there. (3) as a quick test I also tried a small gradle project. The only way I could get it to work was to delegate compile to gradle (Settings > Build, Execution, Deployment > Build Tools > Gradle > Runner -> enable: "Delegate build/run actions to gradle") But this means it's gradle that compiles & runs kapt, not IntelliJ. So now I'm out of ideas. Is there a way to make IntelliJ automatically run kapt as part of building (Ctrl+F9)? Or is basically dagger 2 useless on a maven + Kotlin project? If I have to do "mvn clean package" every time I change a class, that makes dagger 2 useless for me (and for anyone that cares about development productivity). Also, if you use gradle + dagger 2 + kotlin and you delegate build/run actions to gradle, doesn't this make the whole compilation unbearably slow? Especially given the fact that apparently kapt cannot run in gradle incremental mode (https://youtrack.jetbrains.com/issue/KT-11978).
    c

    carwashi

    4 years ago
    The bottom of this section -> https://kotlinlang.org/docs/reference/kapt.html#using-in-maven states the following:
    Please note that kapt is still not supported for IntelliJ IDEA's own build system. Launch the build from the "Maven Projects" toolbar whenever you want to re-run the annotation processing.
    I’ve gotten around this by doing maven builds when changing things related to dagger and using the same target dir in intellij
    c

    cvmocanu

    4 years ago
    @carwashi: thanks a lot. I skimmed that page, but I missed that warning. Your workaround wouldn't work for me since I have around 100 modules and waiting multiple minutes after maven every time I change some dependencies is not acceptable. Again, thanks a lot.