java - Kotlin + Java 9 模块(使用 Java 11)+ maven

标签 java maven kotlin maven-toolchains-plugin

我用 Java 和 Kotlin 代码创建了一个简单的项目,我正尝试将其编译为 Java 9 模块。 这是我的 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>org.example</groupId>
    <artifactId>simple-kotlin-module</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <kotlin.version>1.3.72</kotlin.version>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <classifier>modular</classifier> <!-- FIXME: do we need this? -->
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <compilerVersion>${java.version}</compilerVersion>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <verbose>true</verbose>
                </configuration>
                <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>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>compile</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>11</jvmTarget>
                    <args>
                        <arg>-Xjvm-default=enable</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>toolchain</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <toolchains>
                        <jdk>
                            <version>[${java.version},12)</version>
                        </jdk>
                    </toolchains>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的 module-info.java:

module simple.kotlin.module {
    requires kotlin.stdlib;
}

我不会展示我的代码文件——它们尽可能简单,java 一个包含 main 方法并依赖于 kotlin 一个。

我遇到异常:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.3.72:compile (compile) on project simple-kotlin-module: Compilation failure
[ERROR] Module java.base cannot be found in the module graph

我正在使用 maven 3.6.3,运行 Java 1.8.0_211,所以我必须使用 maven-toolchain-plugin 来选择 java 11。

最佳答案

我也遇到过类似的问题。如果你想要 Kotlin 中的模块,解决方案是避免使用 Java 9 模块并使用 Kotlin 自己的原生模块功能!这是 Kotlin 团队的文档 - https://kotlinlang.org/docs/visibility-modifiers.html#modules

关于java - Kotlin + Java 9 模块(使用 Java 11)+ maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62029932/

相关文章:

java - 无法通过套接字连接到其他设备

java - 如何将参数传递给模拟对象

java - 在运行时访问 OSGi 包中的 pom 属性

Java Apache POI 错误 : XMLEventFactory Provider com. ctc.wstx.stax.WstxEventFactory 未找到

android - Kotlin-共享原始资源中的音频文件

java - sun.jvmstat.monitor.* 在 Java 11 中不可访问

JavaFX:如何将 TextArea 的操作限制在最后一行?

python - 我如何从 Maven 运行 python 代码,其中 python 可能在也可能不在路径中

创建 ArrayList 时 Android Robolectric : OutOfMemoryError - GC overhead limit exceeded,

android - 在 Retrofit Interceptor 中访问 sharedPreferences