java - 在混合 Java/Kotlin 项目中使用 Dagger 2 的 Maven 配置

标签 java maven kotlin dagger-2

使用 Dagger 2 的推荐 Maven 设置是什么?在混合 Java/Kotlin 项目中?

我找到了一个使用 Gradle 的示例项目:https://github.com/damianpetla/kotlin-dagger-example 与 Maven 类似的东西会非常有帮助。


更新:我尝试了什么?

我使用了 kotlinlang.org/docs/reference/using-maven.html 中的 Kotlin 配置 以及来自 google.github.io/dagger 的 Dagger 配置. 我还使用了build-helper-maven-plugin用于集成 IDEA 中的注释处理的插件。

我的主要问题是我遇到了编译周期。我的配置混合了 Kotlin 的编译和调用注释处理器,生成 Dagger2 类。我不系统地尝试将这两个阶段分开,但缺乏更深入的 Maven 理解来使其正常工作。

最佳答案

javac 可以扫描源文件 (java) 和类以搜索注解: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#processing

这意味着如果您没有在 Kotlin 代码中引用任何 Dagger 生成的类(这意味着 Dagger 模块实现),您可以完成这项工作

  1. 调用 kotlin 编译器(Kotlin 代码中没有 Dagger 生成的类型)
  2. 调用注解处理器(处理 java 文件和 kotlin 编译文件中的注解)
  3. 调用 java 编译器 - 可以访问 Dagger 生成的类型和 Kotlin 类型

您可以使用 java 和 kotlin 编写服务,但模块必须由 java 类创建

这里是对应的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>

另一方面,如果您在 kotlin 代码中包含 Dagger 生成的类型,则必须在编译 kotlin 代码之前使这些类型可用,这意味着您需要 Kotlin 感知注释处理器 (KAPT)

在这种情况下,问题归结为以下问题: Is kapt supported in maven?

遗憾的是,答案是否定的,但有一个错误提交来支持它: https://youtrack.jetbrains.com/issue/KT-14478

关于java - 在混合 Java/Kotlin 项目中使用 Dagger 2 的 Maven 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119958/

相关文章:

java - 逗号双数乘法

testing - 质量检查/测试和Maven?

eclipse - 使用 Maven pom.xml 将 War 文件部署到 Tomcat 根目录

安卓错误: Can't find ColorStateList from drawable resource while using dialogs

android-studio - 构建 iosApp : Command PhaseScriptExecution failed with a nonzero exit code 时出现多平台错误

java - 部署 Spring MVC 应用程序时出现异常?

java - 没有新实体的一对多关系

java - 尝试将数据库中的信息显示到终端中

java - 使用命令行从 Nexus 下载具有依赖项的 Maven Artifact

Kotlin 让我崩溃!当我阅读代码时,它是一个函数还是一个类?