1. 程式人生 > >在 Jenkins 中,使用 maven 打包報 package xxx does not exist 問題的解決方法

在 Jenkins 中,使用 maven 打包報 package xxx does not exist 問題的解決方法

1 發現問題

今天打算使用 Jenkins 給一些舊專案打包,編譯時報以下錯誤:

...
[ERROR] /var/lib/jenkins/workspace/fsti-pipe/src/main/java/com/fsti/oss/service/sys/PagerService.java:[6,52] package org.springframework.beans.factory.annotation does not exist
...

看日誌,直接原因是 spring 的 某些 jar 包不存在!

2 尋找原因

在本地開發環境下,用 idea 的 maven 搭建了與 Jenkins 相同的 maven 環境(啟用了新的本地倉庫、使用相同版本的 maven-compiler-plugin),可以正常編譯!這說明一定是其他原因造成的。

在 Jenkins 中,開啟對應專案的【配置】連結,然後點選【構建】頁籤,在 Goals 中為 Maven 加入 -e -X 引數,並儲存:

專案構建配置

這樣會讓 Maven 在構建時打印出更詳細的日誌。

現在專案再重新構建一次,再看看輸出的日誌。

配置的倉庫已經被使用咯,說明倉庫的配置沒有問題:

[DEBUG] Using mirror nexus (http://xxx:8383/nexus/content/groups/public/) for snapshots (http://xxx:8081/nexus/content/repositories/snapshots).
...
[DEBUG] Repositories (dependencies): [nexus-all (http://xxx:8081
/nexus/content/groups/public, default, releases+snapshots), nexus (http://xxx:8383/nexus/content/groups/public/, default, releases+snapshots)] ...

jdk 打包時引用的依賴包路徑有問題,居然會是 /xxx/D:\Program Files\apache-maven\repo... 格式的,這很明顯不是 linux 的正確路徑嘛:

[DEBUG] -d /var/lib/jenkins/workspace/fsti-pipe/target/classes -classpath /var/lib/jenkins/workspace/fsti-pipe/target/classes:/var/lib/jenkins/workspace/fsti-pipe/D:\Program Files\apache-maven\repo/com/fsti/agile/fsti-agile/1.1
.1/fsti-agile-1.1.1.jar: ...

至此可以確定是 maven 的 setting.xml 裡面的 localRepository 配置路徑的不對,造成了上述問題。一看果然是這樣:

localRepository

3 解決問題

把 maven 的 setting.xml 的 localRepository 改為正確的配置路徑即可!記住這個路徑在 linux 中必須有讀寫許可權的哦 O(∩_∩)O~