簡體   English   中英

-source 1.5 錯誤中不支持 java 1.7+

[英]java 1.7+ is not supported in -source 1.5 errors

在 Eclipse 中創建我的 Maven 安裝時,我遇到了 -source 1.5 中不支持的所有類型的 java 錯誤。 我的代碼沒有任何問題。

錯誤如下:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1
[23,62] multi-catch statement is not supported in -source 1.5 
[241,29] try-with-resources is not supported in -source 1.5
[156,64] diamond operator is not supported in -source 1.5

我的 pom 配置如下:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

在 pom.xml 文件中添加以下幾行應該可以解決您的問題。

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

如果您可以考慮提供給您的建議。 您將有 2 個選項可供選擇:

選項 1)如果您保留 maven-war-plugin。 將版本更新到最新,然后添加帶有編譯器信息和 sourceEncoding 的屬性,但刪除配置行

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    
    ..
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
    </plugin>
    ..
</build>

選項 2)如果將 maven-war-plugin 替換為 maven-compiler-plugin ** 無需在屬性中添加/替換源、目標和編碼**。 確保將版本更新到最新:

<build>
    ..
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
    ..
</build>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM