java - 在 Java : Cannot find symbol 中使用 Kotlin 类

我找到了this关于 Android 的类似问题,但我使用纯 Java 和 Maven 作为构建工具。我认为最好发布一个新问题。

我创建了一个 Kotlin 类,我试图从 Java 类中将其引用为 MyKotlinClass.class。 Maven 构建失败,而 IntelliJ Idea 中的编译工作正常。我已经在 maven 中添加了 Kotlin 插件:

        <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>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

但这无济于事:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project app: Compilation failure
[ERROR] MyClassLinkingKotlin.java:[100,40] error: cannot find symbol

行/列完全引用符号MyKotlinClass.class。即使这样使用也会失败:

System.err.println(MyKotlinClass.class)

最佳答案

您的 Maven 配置添加了 Kotlin 编译器插件,但没有调整 Java 编译器插件的执行,以便 Java 编译器在 Kotlin 编译器之后运行。因此,Java 编译器在 Kotlin 之前运行,并且看不到 Kotlin 编译的类。

这是一个显示混合语言项目正确配置的片段(取自 the documentation):

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

https://stackoverflow.com/questions/43980110/

相关文章:

visual-studio-2010 - 如何在VS2010后期构建步骤中添加双引号

c# - 使用不同的 DLL 进行调试和发布版本

c++ - 使用 Xcode 在 Mac 上构建 Boost

scala - SBT:如何访问环境变量或配置?

c++ - CMake - 创建一个静态库

java - Intellij 在 jar 文件中包含 src 文件

android - 使用 Gradle 的外部 Android 库项目

xcode - 如何强制 xcode 在特定文件上使用 ARC?

jenkins - 在 Jenkins 中,构建如何知道谁请求了它们?

build - 如何在 Intellij IDEA 的构建路径中重新包含一个类?