使用Maven编译Java项目失败

15

我正在尝试在Java 16.0.1上使用Maven构建IntelliJ项目,但它无法编译我的项目,尽管IntelliJ能够成功完成。 在此之前,我使用maven编译了一个Java 15项目,但由于机器上不同版本的java完全混乱,例如我能够编译但无法运行生成的.jar文件等等,因此决定将所有内容更新为16.0.1。
mvn compile 输出:

[ERROR] Error executing Maven.
[ERROR] java.lang.IllegalStateException: Unable to load cache item
[ERROR] Caused by: Unable to load cache item
[ERROR] Caused by: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper

这是我的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>TaxiDB</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>16.0.1</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        ...
    </dependencies>
</project>

mvn -version 的输出结果:

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-16-oracle
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.8.0-50-generic", arch: "amd64", family: "unix"

java -verion 的输出结果为:

java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

我认为release应该是16,而不是16.0.1 - J Fabian Meier
@JFabianMeier,已尝试。输出相同。 - denh
我发现这个错误不仅会在'mvn compile'命令中出现,而且会在每个mvn命令中出现。 - denh
我在升级到jdk 16 (maven 3.6)后,也遇到了完全相同的问题。正在尝试升级到maven 3.8。 - ACV
2
升级到maven 3.8解决了问题。在Ubuntu上,只有通过apt安装3.6版本,因此您需要手动安装它。这是一个非常好的指南:https://linuxize.com/post/how-to-install-apache-maven-on-ubuntu-20-04/ (但是,在脚本中JAVA_HOME路径有一个错误)。 - ACV
显示剩余3条评论
2个回答

14

问题在于你的Maven版本太旧了,与JDK 16不兼容。

尝试手动安装Maven版本3.8.1:

https://maven.apache.org/install.html

这样就可以解决问题而无需降级JDK版本。


尝试使用sdkman安装JDK和Maven,这将使您更轻松地管理JDK和工具版本。 - Rafael Carrillo
这对我也解决了问题。谢谢! - Ryan

0

在Ubuntu 20.10上运行mvn compile时,我遇到了这个问题。但是在最新版本的Ubuntu 21.04上运行相同的Maven命令似乎没有问题。

您可能需要检查正在运行的Linux版本。如果运行的是旧版本,请升级到Ubuntu 21.04。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接