将Kotlin与生成的Java类集成

Mar*_*eln 2 annotation-processing kotlin immutables-library

我们使用注释处理框架(Immutables)从接口生成Java类.

现在我必须从kotlin类访问这些生成的类.虽然在Java中这很好用,但Kotlin编译器却找不到它们.

这是maven配置:

        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/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>${project.basedir}/src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <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>
Run Code Online (Sandbox Code Playgroud)

Kotlin版本是1.2.10(但它与1.2.20相同).交换插件声明没有帮助.我该如何配置?

tyn*_*ynn 5

科特林编译器之前运行的Java编译器.要使用Kotlin创建已创建的类,必须使用kapt并在那里定义每个注释处理器.

<execution>
    <id>kapt</id>
    <goals>
        <goal>kapt</goal>
    </goals>
    <configuration>
        <sourceDirs>
            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
            <sourceDir>${project.basedir}/src/main/java</sourceDir>
        </sourceDirs>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.immutables</groupId>
                <artifactId>value</artifactId>
                <version>2.5.5</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</execution>
Run Code Online (Sandbox Code Playgroud)

  • 解决了.我在kapt中发现了一个与immutables结合的奇怪错误:我们对不可变的使用自定义注释.这些必须在文件`src/main/resources/META-INF/annotations/org.immutables.value.immutable`中配置.但是,所有具有自定义注释的类都不会生成那些自定义注释由该项目所依赖的maven项目提供的类.如果我将自定义注释添加到其他项目中的文件,则它可以正常工作.*这意味着kapt不使用给定src/main/java文件夹中的文件,而只使用类路径中的那个文件!* (2认同)