【问题标题】:Unable to compile java project with maven无法用maven编译java项目
【发布时间】:2021-07-23 20:16:33
【问题描述】:

我正在尝试在 java 16.0.1 上使用 maven 构建 IntelliJ 项目,但它无法编译我的项目,尽管 IntelliJ 能够成功完成它。在此之前,我使用 maven 编译了一个 java 15 项目,但我决定将所有内容更新到 16.0.1,因为我的机器上的不同版本的 java 完全混乱,就像我能够编译但不能运行生成的 .jar 文件等等
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
  • @JFabianMeier,试过了。相同的输出。
  • 不确定这是否对您有帮助,但您没有为 maven-jar-plugin 指定版本
  • @Lino,没用
  • 升级到 maven 3.8 解决了这个问题。在 Ubuntu 上,只有 3.6 可通过 apt 获得,因此您必须手动安装它。这是一个相当不错的指南:linuxize.com/post/how-to-install-apache-maven-on-ubuntu-20-04(但是脚本中的JAVA_HOME路径有错误)

标签: java maven ubuntu compiler-errors maven-compiler-plugin


【解决方案1】:

问题是您的 Maven 版本太旧。它与 JDK 16 不兼容。

尝试手动安装maven 3.8.1版本:

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

这将解决您的问题,而无需降级您的 JDK 版本。

【讨论】:

  • 是的,有帮助 谢谢
  • 尝试使用 sdkman 安装 JDK 和 Maven,这样可以更轻松地管理您的 JDK 和工具版本
  • 这也为我解决了这个问题。谢谢!
【解决方案2】:

我在 Ubuntu 20.10 上运行 mvn compile 时遇到了这个问题。在 Ubuntu 21.04(最新版本)上运行相同的 maven 命令似乎没问题。

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

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 2021-06-30
    • 2018-08-30
    • 2014-08-18
    • 2014-07-17
    • 2015-03-14
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多