Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

IntelliJ IDEA 中 Maven 未配置 JDK 的版本問題與解法

在 IntelliJ IDEA 14.1 裡使用 Maven 管理 Java 專案時,如果沒有在 Maven 中明確配置編譯版本與源碼版本,IDEA 會把預設值設為相當老的 JDK 5。這會導致 Language level 與目標版本都被鎖定在 1.5,進而反覆出現「源值 1.5 已過時」「目標值 1.5 已過時」這類警告。

更麻煩的是,即使你手動把等級改成 JDK 8,只要重新載入 Maven 專案,IDEA 又會把 Language level 與 Target bytecode version 打回 JDK 5。

解決方案

方式一,在專案的 pom.xml 中加入 properties 段,明確指定版本:

1 <properties>
2  <maven.compiler.source>1.8</maven.compiler.source>
3  <maven.compiler.target>1.8</maven.compiler.target>
4 </properties>

方式二,修改 Maven 的 Settings.xml,加入一個預設啟用的 profile:

 1 <profile>
 2  <id>jdk-1.8</id>
 3  <activation>
 4   <activeByDefault>true</activeByDefault>
 5   <jdk>1.8</jdk>
 6  </activation>
 7  <properties>
 8   <maven.compiler.source>1.8</maven.compiler.source>
 9   <maven.compiler.target>1.8</maven.compiler.target>
10   <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
11  </properties>
12 </profile>

完成任一設定後,IDEA 就會依據 Maven 中配置的版本來決定源碼與目標 JDK 版本,重新載入專案時也不會再被打回 1.5。建議優先採用方式一,把設定寫進專案本身的 pom.xml,讓團隊成員都能獲得一致的建置環境。

Copyright © Linux教程網 All Rights Reserved