我在部署構件至 maven nexus 私服時,有時會出現 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 類似這樣的錯誤,那么這些錯誤是怎么產生,又如何解決呢?我在此將自己在部署過程中遇到的錯誤整理匯總一下,供大家參閱,希望對大家有所幫助。
一、錯誤的請求。Return code is: 400, ReasonPhrase: Bad Request.
具體錯誤信息如下所示:
[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.135 s [INFO] Finished at: 2016-02-18T10:23:58+08:00 [INFO] Final Memory: 19M/174M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger: Failed to deploy artifacts: Could not transfer artifact nl.flotsam:xeger:jar:1.0.2 from/to nexus-releases (http://localhost:8081/nexus/content/repositories/releases/): Failed to transfer file: http://localhost:8081/nexus/content/repositories/releases/nl/flotsam/xeger/1.0.2/xeger-1.0.2.jar. Return code is: 400, ReasonPhrase: Bad Request. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
400錯誤的含義是“錯誤的請求”,在這里的原因是往往是沒有部署到nexus的倉庫中。產生原因有如下兩種:
1、部署倉庫錯誤:
前面的博文有講過 Nexus 私服有三種倉庫類型:Hosted、Proxy和Virtual,另外還有一個 group (倉庫組)用於對多個倉庫進行組合。部署的時候只能部署到 Hosted 類型的宿主倉庫中,如果是其他類型就會出現這個 400 錯誤。若是出現這個錯誤,只需修改 POM 文件中的部署倉庫到對應的宿主倉庫即可解決此問題。
2、宿主倉庫不允許重復部署:
默認情況下重復部署構件到 Releases 倉庫中也會出現 400 錯誤,原因是 Nexus 私服中 Releases 倉庫默認的 Deployment Policy 是 “Disable Redeploy”,所以當你重復部署構件至 Releases 宿主倉庫時就會出現這個 400 錯誤。出現這個問題,只需要將宿主倉庫的 Deployment Policy 改為 “” 即可解決,解決方法如下所示:
二、未認證或認證不通過。Return code is: 401, ReasonPhrase: Unauthorized.
[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.215 s [INFO] Finished at: 2016-02-18T10:50:56+08:00 [INFO] Final Memory: 19M/175M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger: Failed to deploy artifacts: Could not transfer artifact nl.flotsam:xeger:jar:1.0.2 from/to nexus-releases (http://localhost:8081/nexus/content/repositories/releases/): Failed to transfer file: http://localhost:8081/nexus/content/repositories/releases/nl/flotsam/xeger/1.0.2/xeger-1.0.2.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
出現這種錯誤,原因如下所示:
1、是未配置 Nexus 私服用戶信息,或配置的賬號、密碼錯誤,均會出現 401 的錯誤,解決方法就是修改 maven settings.xml 配置文件,添加如下信息即可解決問題。
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> ... <servers> <!-- Nexus 構件部署用戶信息 --> <server> <id>nexus-releases</id> <username>fanfengping</username> <password>密碼</password> </server> <server> <id>nexus-snapshots</id> <username>fanfengping</username> <password>密碼</password> </server> </servers> ... </settings>
2、是 maven 項目工程 POM 文件中配置的 Nexus 私服宿主倉庫的 project.distributionManagement.[repository|snapshotRepository].id 在 maven settings.xml 中配置的宿主倉庫 settings.servers.server.id 不存在,統一二者的 ID 后,即可解決此問題。
三、連接失敗。Connection refused: connect. 或 Return code is: 405
出現這種錯誤,肯定是 POM 文件中配置的 url 錯誤,認真檢查一下 url 是否正確,然后訂正一下,重新部署即可。
四、缺失私服倉庫連接配置。Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
具體提示信息如下所示:
[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.969 s [INFO] Finished at: 2016-02-18T10:47:22+08:00 [INFO] Final Memory: 18M/177M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
此問題是因 POM 文件遺漏了 Nexus 私服連接配置導致的,在 POM 文件中添加 distributionManagement 節點或者在命令行執行時添加 -DaltDeploymentRepository=id::layout::url 參數即可解決。POM 文件中添加 distributionManagement 如下所示:
<project> ... <developers> ... </developers> <distributionManagement> <repository> <id>nexus-releases</id> <name>Nexus Releases Repository Pro</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshots Repository Pro</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> <dependencies> ... </dependencies> <build> ... </build> </project>
至此, Maven-008-Nexus 私服部署發布報錯 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 解決方案 順利完結,希望此文能夠給初學 Maven 的您一份參考。
最后,非常感謝親的駐足,希望此文能對親有所幫助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^