致命错误编译:使用IntelliJ IDEA和Maven不支持发行版本10.0.1

时间:2020-04-05 00:09:19

标签: java maven intellij-idea junit

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

当我进入终端并输入:

"    CJJCC"

标准输出:

echo $JAVA_HOME

Java版本:

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

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

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

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"

计算器类:

<?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>

JUnit 5测试:

package com.sample;

public class Calculator {

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

当我通过Terminal或IntelliJ IDEA Ultimate Edition 2019.3(作为Maven运行配置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)); } } )运行它时,出现以下错误:

mvn test

1 个答案:

答案 0 :(得分: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 (在macOS上)以执行替换操作,在搜索字段中输入10.0.1,在 Tab 中,键入10 全部替换的替换字段 Alt + A