1. 程式人生 > >Maven打包異常Unable to locate the Javac Compiler in:Please ensure you are using JDK 1.4 or above and

Maven打包異常Unable to locate the Javac Compiler in:Please ensure you are using JDK 1.4 or above and

最近maven打包專案時出現以下異常:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project com.xxx: Compilation failure

Unable to locate the Javac Compiler in:
E:\JDK1.8\jdk_1.8.0_20\..\lib\tools.jar
Please ensure you are using JDK 1.4 or above and not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java installation by setting the JAVA_HOME environment variable.

原因:專案打包使用了以下外掛,maven打包時會根據生命週期執行外掛,以下外掛在編譯時執行,不設定版本時預設為2.3.2,由於個人的Java版本為1.8的,而該外掛版本2.3.2太舊所以不支援1.8,直接到http://search.maven.org搜maven-compiler-plugin把copy最新版本即可解決問題(當然也不一定需要最新的)。

當然也不排除其它原因(如環境變數,但這種情況比較小),這只是個人遇到的問題解決方法

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> </configuration> </plugin>