javac có thể quét tất cả các file nguồn (java) và các lớp học để tìm kiếm cho chú thích: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#processing
Điều này có nghĩa rằng bạn có thể làm cho công việc này nếu bạn không có bất kỳ lớp học Dagger tạo tham chiếu trong mã Kotlin (có nghĩa là triển khai Dagger mô-đun)
- invoke biên dịch Kotlin (không Dagger tạo kiểu trong mã Kotlin) xử lý chú thích
- invoke (xử lý các chú thích trong cả file java và các tập tin Kotlin-biên soạn)
012.
- invoke biên dịch java - có quyền truy cập vào cả hai loại Dagger tạo và các loại Kotlin
Bạn có thể viết các dịch vụ của bạn trong cả java và Kotlin, nhưng các module phải được tạo ra bởi một lớp java
Đây là 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>
tương ứng Mặt khác, nếu bạn bao gồm các loại Dagger tạo trong mã Kotlin của bạn, bạn phải có những sẵn trước khi mã Kotlin được biên dịch, có nghĩa là bạn cần phải xử lý chú thích Kotlin-aware (KAPT)
Trong kịch bản này, các vấn đề sôi xuống cho câu hỏi: Is kapt supported in maven?
Đáng buồn thay, câu trả lời là không, nhưng có một lỗi nộp để hỗ trợ nó: https://youtrack.jetbrains.com/issue/KT-14478
FYI KT-14.478 đã được cố định và do đó kapt bây giờ được hỗ trợ trong maven –
@Markus Cảm ơn bạn đã cập nhật – Lesiak