【问题标题】:Fatal error compiling: release version 10.0.1 not supported using IntelliJ IDEA and Maven致命错误编译:不支持使用 IntelliJ IDEA 和 Maven 发布版本 10.0.1
【发布时间】:2020-04-05 00:09:19
【问题描述】:

在 macOS Catalina 10.15.4 上,我安装了 jdk-10.0.1。

当我去终端输入:

echo $JAVA_HOME

标准输出:

/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home

Java 版本:

java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

mvn -version
mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/pnwlover/maven/apache-maven-3.6.3
Java version: 10.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "mac"

创建了一个示例 maven 项目并将 JUnit 5 设置为依赖项:

<?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>com.sample</groupId>
    <artifactId>Calculator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>10.0.1</java.version>
        <maven.compiler.source>10.0.1</maven.compiler.source>
        <maven.compiler.target>10.0.1</maven.compiler.target>
        <junit-jupiter.version>5.3.2</junit-jupiter.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>10.0.1</release>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>6.2</version> 
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>6.1.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

计算器类:

package com.sample;

public class Calculator {

    public static int add(int a, int b) {
        return a + b;
    }
}

JUnit 5 测试:

package com.sample;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @Test
    public void add() {
        assertEquals(5, Calculator.add(2, 2));
    }

}

当我通过终端或 IntelliJ IDEA Ultimate Edition 2019.3(作为 Maven 运行配置 mvn test)运行它时,我收到以下错误:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.sample:Calculator >----------------------
[INFO] Building Calculator 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Calculator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ Calculator ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/pnwlover/Calculator/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.655 s
[INFO] Finished at: 2020-04-04T16:57:35-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project Calculator: Fatal error compiling: release version 10.0.1 not supported -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

【问题讨论】:

    标签: java maven intellij-idea junit


    【解决方案1】:

    不支持指定为10.0.1 的Java 源/目标版本。您应该只使用主要版本,例如 10

    <java.version>10</java.version>
    <maven.compiler.source>10</maven.compiler.source>
    <maven.compiler.target>10</maven.compiler.target>
    
    <configuration>
        <release>10</release>
    </configuration>
    

    在 IntelliJ IDEA 打开 pom.xml 文件,按 Ctrl+R (Alt/Option+R on macOS)对于 Replace 操作,在搜索字段中键入 10.0.1Tab,在替换字段中键入 10Alt+A 表示全部替换

    【讨论】:

    • 感谢@CrazyCoder - 这工作!您是如何在答案中添加这些按钮的?
    • @PacificNW_Lover &lt;kbd&gt; 标签,我使用这个扩展名:stackapps.com/questions/3341/…
    • 谢谢,但谷歌浏览器不允许我安装它...你如何使用/安装这个 JavaScript 文件和 Stack Overflow?
    • CrazyCoder - 通过 TamperMonkey 安装它,当我创建一个新问题时,我可以看到该小部件,但 &lt;kbd&gt;hello&lt;/kbd&gt; 在 cmets 文本字段内内联不起作用。
    猜你喜欢
    • 2021-05-09
    • 2021-03-08
    • 2019-01-08
    • 2020-04-23
    • 2011-07-31
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    相关资源
    最近更新 更多