在Kotlin项目中包括使用Java9或Java10构建的Maven依赖项

约翰尼斯:

是否可以包含Java9(或10)依赖项?由于Kotlin到目前为止只能编译到Java 8。java.lang.UnsupportedClassVersionError显然收到一条错误消息,指出我包含的JAR /依赖项中的类已由Java Runtime的较新版本进行编译。但是我不能指定一个新的JDK,它可以运行用旧版本编译的类(Kotlin的东西吗?)。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.7.0</version>
  <configuration>
    <release>9</release>
    <source>${maven.compiler.source}</source>
    <target>${maven.compiler.target}</target>
    <encoding>${project.build.sourceEncoding}</encoding>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.ow2.asm</groupId>
      <artifactId>asm</artifactId>
      <version>6.1</version> <!-- Use newer version of ASM -->
    </dependency>
  </dependencies>
</plugin>

与属性

<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>

但是现在我得到了错误: "Error:Kotlin: Unknown JVM target version: 1.9 Supported versions: 1.6, 1.8"

Kotlin版本是1.2.41,到目前为止,我一直可以切换到最新版本,直到它发布为止...

可以使用下面的XML片段。但是问题java.lang.UnsupportedClassVersionError仍然存在。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </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>

我正在尝试为我的(当前)Java9项目构建Kotlin模块/ Maven包。

Maven-Kotlin插件配置为:

      <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <configuration>
                <jvmTarget>1.8</jvmTarget>
            </configuration>
            <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>
                    <configuration>
                        <sourceDirs>
                            <source>src/test/java</source>
                            <source>src/test/kotlin</source>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我的整个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>restful-web</groupId>
    <artifactId>restful-web</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <kotlin.version>1.2.41</kotlin.version>
        <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
        <sirix.version>0.8.9-SNAPSHOT</sirix.version>
        <main.verticle>org.sirix.rest.SirixVerticle</main.verticle>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-lang-kotlin</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.sirixdb.sirix</groupId>
            <artifactId>sirix-core</artifactId>
            <version>${sirix.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.sirixdb.sirix</groupId>
            <artifactId>sirix-xquery</artifactId>
            <version>${sirix.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </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.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>io.vertx.core.Launcher</Main-Class>
                                        <Main-Verticle>${main.verticle}</Main-Verticle>
                                    </manifestEntries>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
                                </transformer>
                            </transformers>
                            <artifactSet>
                            </artifactSet>
                            <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
                <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>
                        <configuration>
                            <sourceDirs>
                                <source>src/test/java</source>
                                <source>src/test/kotlin</source>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>kotlin-bintray</id>
            <name>Kotlin Bintray</name>
            <url>http://dl.bintray.com/kotlin/kotlin-dev</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <repositories>
        <repository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

错误消息/ stacktrace的相关部分是:

java.lang.UnsupportedClassVersionError: org/sirix/access/conf/DatabaseConfiguration has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0

亚历山大·乌达洛夫(Alexander Udalov):

我怀疑问题出在Kotlin Maven插件中,该插件-jvm-target直接从maven.compiler.source/ maven.compiler.targetproperties 推断参数值,而不考虑Kotlin编译器仅支持1.6和1.8版本的事实。解决方法是将JVM字节码目标版本明确指定为1.8,例如(请注意,以1.8为目标的字节码可以在Java 9上正常运行):

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>
    ...
    <configuration>
        <jvmTarget>1.8</jvmTarget>
    </configuration>
</plugin>

就是说,我无法在一个小样本项目中重现您的问题,因此,如果您有时间,请在https://kotl.in/issue中报告问题,我们将不胜感激。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Java转换为Maven的项目中缺少Maven依赖项

无法处理Java9项目中的文件module-info.class导致ClassFormatException

Kotlin + Java 项目中 Eclipse 中的编译错误,但项目使用 Maven 构建

如何使用Maven将Clojure依赖项包含到Java项目中

分析Java项目中的JAR依赖项

如何配置Pom文件和TeamCity在Maven Java项目中构建依赖模块?

如何在Java项目中引用Maven依赖项的单元测试类?

将 Maven 依赖项导入 Java 项目

IntelliJ项目中的Maven依赖项

在Maven项目中查找依赖项(反向依赖项)

Web项目中的Java EE模块依赖项?

使用Java库插件在gradle项目中复制所有依赖项

如何使用Eclipse将依赖项打包到Java项目中

Java项目依赖项

我需要什么来构建JDK 9项目,使用Maven非模块化的依赖

如何在GWT Maven项目中使用简单的Maven项目作为依赖项?

将Maven项目添加为Maven项目中的依赖项

项目无法在 Dependent 项目中使用 SDK 依赖项构建

我可以在纯Java项目中使用由Kotlin协程构建的库吗?

我可以在Java 7中编译的项目中使用Java 7中编译的jar作为依赖项吗?

使用Java的ServiceLoader构建与jdk8使用> = java9

如何在使用Maven构建的战争中包括系统依赖项

使用Maven使用OpenCV构建Java项目

如何将焊缝嵌入码头9中以在我的java maven项目中使用cdi?

Maven同时使用Maven依赖项和引用库来构建项目

如何在Spring-boot多模块Java 11项目中添加Spark依赖项

使用Maven的Android项目中来自APK依赖项的'Unresolved type'

父项目包括常见的Maven依赖项

具有本地依赖项的Maven项目中的NoClassDefFoundError