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

e3bfsja2  于 2022-11-02  发布在  Maven
关注(0)|答案(2)|浏览(219)

在混合Java/Kotlin项目中使用Dagger 2时,建议的Maven设置是什么?
我找到了一个使用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和调用注解处理器,后者生成Dagger 2类。我没有系统地尝试将这两个阶段分开,但缺乏对Maven更深入的理解,无法使其正常工作。

mitkmikd

mitkmikd1#

javac可以扫描源文件(Java)和类以搜索注解:http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#processing
这意味着,如果Kotlin代码中没有引用任何Dagger生成的类(这意味着Dagger模块实现),也可以使用它。
1.调用Kotlin编译器(Kotlin代码中没有Dagger生成的类型)
1.调用注解处理器(处理java文件和kotlin编译的文件中的注解)
1.调用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?
遗憾的是,答案是否定的,但有一个bug可以支持它:https://youtrack.jetbrains.com/issue/KT-14478

fnx2tebb

fnx2tebb2#

由于maven现在支持kapt,所以你可以用Java和Kotlin编写服务,模块可以由Java类或Kotlin类创建。

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>1.7.20</version>
    </dependency>
    <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>2.22</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>kapt</id>
                    <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.22</version>
                            </annotationProcessorPath>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/main/kotlin</sourceDir>
                            <sourceDir>src/main/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/test/kotlin</sourceDir>
                            <sourceDir>src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <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>

当你想用java,Kotlin和dagger创建maven项目时,可以阅读下面的链接:

请注意,IntelliJ IDEA自己的构建系统仍然不支持kapt。因此,您将无法通过单击Main方法旁边的Play按钮在Intellij IDE中编译和运行应用程序。要运行应用程序,请首先使用maven工具箱构建项目。然后,您可以通过单击Main方法旁边的Play按钮在Intellij IDE中运行应用程序。2或者,您可以创建一个运行配置来运行jar文件。3之后,您可以运行此配置,或者通过单击运行配置组合框旁边的运行或调试按钮以调试模式运行它。

相关问题