1. 程式人生 > 實用技巧 >關於Maven專案build時出現No compiler is provided in this environment的處理(轉)

關於Maven專案build時出現No compiler is provided in this environment的處理(轉)

近日有同事遇到在編譯Maven專案時出現
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
的問題, 原以為這是個個例, 源於同事粗心, 配置環境出問題造成, 後到百度檢視一下, 遇到這個問題的不在少數, 但是對問題的解釋沒有說到根源, 於是寫下這篇部落格供大家參閱, 如有紕漏, 還請指正.

錯誤程式碼節選:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.436 s
[INFO] Finished at: 2017-06-28T11:16:07+08:00
[INFO] Final Memory: 10M/151M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project manage: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]


但是編寫普通Java Project編譯執行卻是正常的,下圖為只有輸出語句的普通java類

從上圖中可以看出, java編譯環境未jre1.7.0_17, 也就是說並沒有配置成jdk目錄, 然後看Eclipse-->Window-->preferences-->Java-->Installed JREs

為了演示出效果, 在測試之前, 我已經將系統java環境配置成如上圖所示路徑, 並只保留該配置, 由下圖可以看出, 該路徑是我所安裝的兩個JDK版本中的一個JDK自帶的jre執行環境. 使用該環境編譯普通專案沒有問題, 但為什麼會在編譯Maven專案時出錯呢?

我們看看Maven的環境是如何配置的:先找到Eclipse-->Window-->preferences-->Maven-->Installations

在Maven配置中, 我並沒有使用Eclipse自帶的Maven外掛, 而是重新配置的Maven環境, 然後再看Eclipse-->Window-->preferences-->Maven-->User Settings

Maven設定使用的是Maven中conf資料夾下的settings.xml, 點選"open file" 在Eclipse中檢視具體配置資訊, 僅摘錄與本錯誤資訊相關的部分

  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.-->
    
    <profile>
    	<id>jdk-1.7</id>
    	<activation>
    		<activeByDefault>true</activeByDefault>
    		<jdk>1.7</jdk>
    	</activation>
    	<properties>
			<maven.compiler.source>1.7</maven.compiler.source>
			<maven.compiler.target>1.7</maven.compiler.target>
			<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
		</properties>
    </profile>
  </profiles>


中間具體資訊的理解, 可以參見 冰河winner的部落格. 也就是說, 根據上面的配置, 我們需要指定一個符合配置的JDK環境, 這是我們之前在Eclipse-->Window-->preferences-->Java-->Installed JREs下的配置就不行了, 而需要指定一個JDK目錄, 例如我的JDK安裝目錄下的jdk1.7.0_17, 這也是這個錯誤出現的罪魁禍首. 不過對於Java開發者來說, Installed JREs中使用jdk目錄而不適用jre目錄也是最好的選擇.

步驟:

然後再編譯執行專案即可.