Maven configuration for using Dagger 2 in a mixed Java / Kotlin project

What is the recommended Maven setup for using Dagger 2 in a mixed Java / Kotlin project?

I found a sample project that uses Gradle: https://github.com/damianpetla/kotlin-dagger-example Something like Maven would be very helpful.


UPDATE: what have I tried?

I used the Kotlin configuration from kotlinlang.org/docs/reference/using-maven.html and the dagger configuration from google.imtqy.com/dagger . I also used the build-helper-maven-plugin plugin to integrate annotation processing into IDEA.

My main problem was that I ran into compilation cycles. My configuration mixed Kotlin compilation and called the annotation processor that generates the Dagger2 classes. I tried unsystematically to separate both phases, but did not need a deeper understanding of Maven to make it work.

+5
source share
1 answer

javac can scan both source files (java) and classes looking for annotations: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#processing

This means that you can do this work if you do not have the classes created by the dagger that the Kotlin code refers to (which means the implementation of the Dagger modules)

  • call the kotlin compiler (no codes of generated types in Kotlin code)
  • call annotation handler (processes annotations both in java files and in files compiled by kotlin)
  • invoke java compiler - has access to both dagger-generated types and Kotlin types

You can write your services in both java and kotlin, but the module must be created using the java class

Here is the corresponding pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>testkotlindagger</artifactId> <version>1.0-SNAPSHOT</version> <properties> <kotlin.version>1.0.6</kotlin.version> <dagger2.version>2.7</dagger2.version> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> <!-- Dagger 2 --> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger</artifactId> <version>${dagger2.version}</version> </dependency> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger-compiler</artifactId> <version>${dagger2.version}</version> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <sourceDirs> <source>src/main/java</source> <source>src/main/kotlin</source> </sourceDirs> </configuration> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>2.2.4</version> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>compile</phase> <configuration> <outputDirectory>target/generated-sources/annotations</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <executions> <!-- Replacing default-compile as it is treated specially by maven --> <execution> <id>default-compile</id> <phase>none</phase> </execution> <!-- Replacing default-testCompile as it is treated specially by maven --> <execution> <id>default-testCompile</id> <phase>none</phase> </execution> <execution> <id>java-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>java-test-compile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 

On the other hand, if you include types generated by the dagger in your kotlin code, you must have them available before compiling the kotlin code, which means you need the Kotlin comment handler (KAPT)

In this scenario, the problem boils down to the question: Is kapt supported in maven?

Unfortunately, the answer is no, but there is an error filed to support it: https://youtrack.jetbrains.com/issue/KT-14478

+1
source

Source: https://habr.com/ru/post/1237514/


All Articles