1. 程式人生 > >Jenkins部署Maven專案時提示找不到JDK問題的解決方案

Jenkins部署Maven專案時提示找不到JDK問題的解決方案

背景描述

今天我將一個Maven Web專案在Jenkins中配置自動構建部署時,遇到報錯:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project personal: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

一開始不明白指的什麼意思,在stackoverflow上查了一下,找到了這樣一個連結,雖然提問者問的是Eclipse中Maven執行的問題,但是最後面提問者的解決方案給我了一個啟發。

具體細節和解決方案

這肯定是Maven在構建時查詢JDK遇到了障礙,可能是某處環境變數配置沒有到位。我pom.xml中關於maven-compiler-plugin編譯外掛的內容是這樣的:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <compilerArguments>
            <endorseddirs>${endorsed.dir}</endorseddirs>
        </compilerArguments>
    </configuration>
</plugin>

stackoverflow上的答案給我的啟發就是我這裡<configuration>中的JDK配置不明確,需要指定具體的地址:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <fork>true</fork>
        <executable>C:\Program Files\Java\jdk1.8.0_66\bin\javac.exe</executable>
    </configuration>
</plugin>

在將pom.xml修改之後,Jenkins確實構建成功了。

這個問題和我之前寫的部落格使用Ant工程建立Jenkins Job時可能遇到的幾個問題中的"2. Java EE server問題"有相似之處,那邊需要明確的指明Server所在目錄,這裡也需要明確的指明JDK所在目錄。