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


本文轉自https://blog.csdn.net/lslk9898/article/details/73836745

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

錯誤代碼節選:

 

[java]  view plain  copy
 
  1. [ERROR] COMPILATION ERROR :   
  2. [INFO] -------------------------------------------------------------  
  3. [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?  
  4. [INFO] 1 error  
  5. [INFO] -------------------------------------------------------------  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO] BUILD FAILURE  
  8. [INFO] ------------------------------------------------------------------------  
  9. [INFO] Total time: 1.436 s  
  10. [INFO] Finished at: 2017-06-28T11:16:07+08:00  
  11. [INFO] Final Memory: 10M/151M  
  12. [INFO] ------------------------------------------------------------------------  
  13. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project manage: Compilation failure  
  14. [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?  
  15. [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中查看具體配置信息, 僅摘錄與本錯誤信息相關的部分

 

[html]  view plain  copy
 
  1. <profiles>  
  2.   <!-- profile  
  3.    | Specifies a set of introductions to the build process, to be activated using one or more of the  
  4.    | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>  
  5.    | or the command line, profiles have to have an ID that is unique.  
  6.    |  
  7.    | An encouraged best practice for profile identification is to use a consistent naming convention  
  8.    | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.  
  9.    | This will make it more intuitive to understand what the set of introduced profiles is attempting  
  10.    | to accomplish, particularly when you only have a list of profile id's for debug.  
  11.    |  
  12.    | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.-->  
  13.     
  14.   <profile>  
  15.     <id>jdk-1.7</id>  
  16.     <activation>  
  17.         <activeByDefault>true</activeByDefault>  
  18.         <jdk>1.7</jdk>  
  19.     </activation>  
  20.     <properties>  
  21.     <maven.compiler.source>1.7</maven.compiler.source>  
  22.     <maven.compiler.target>1.7</maven.compiler.target>  
  23.     <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>  
  24. </properties>  
  25.   </profile>  
  26. </profiles>  


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

 

步驟:

然后再編譯運行項目即可.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM