我在部署構件至 maven nexus 私服時,有時會出現 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 相似這樣的錯誤,那麼這些錯誤是怎麼產生,又如何解決呢?我在此將本身在部署過程當中遇到的錯誤整理彙總一下,供你們參閱,但願對你們有所幫助。apache
1、錯誤的請求。Return code is: 400, ReasonPhrase: Bad Request.maven
具體錯誤信息以下所示: ide
[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的倉庫中。產生緣由有以下兩種:ui
一、部署倉庫錯誤:url
前面的博文有講過 Nexus 私服有三種倉庫類型:Hosted、Proxy和Virtual,另外還有一個 group (倉庫組)用於對多個倉庫進行組合。部署的時候只能部署到 Hosted 類型的宿主倉庫中,若是是其餘類型就會出現這個 400 錯誤。如果出現這個錯誤,只需修改 POM 文件中的部署倉庫到對應的宿主倉庫便可解決此問題。spa
二、宿主倉庫不容許重複部署:
默認狀況下重複部署構件到 Releases 倉庫中也會出現 400 錯誤,緣由是 Nexus 私服中 Releases 倉庫默認的 Deployment Policy 是 「Disable Redeploy」,因此當你重複部署構件至 Releases 宿主倉庫時就會出現這個 400 錯誤。出現這個問題,只須要將宿主倉庫的 Deployment Policy 改成 「」 便可解決,解決方法以下所示:命令行
2、未認證或認證不經過。Return code is: 401, ReasonPhrase: Unauthorized.debug
[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
出現這種錯誤,緣由以下所示:code
一、是未配置 Nexus 私服用戶信息,或配置的帳號、密碼錯誤,均會出現 401 的錯誤,解決方法就是修改 maven settings.xml 配置文件,添加以下信息便可解決問題。 orm
<?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>
二、是 maven 項目工程 POM 文件中配置的 Nexus 私服宿主倉庫的 project.distributionManagement.[repository|snapshotRepository].id 在 maven settings.xml 中配置的宿主倉庫 settings.servers.server.id 不存在,統一兩者的 ID 後,便可解決此問題。
3、鏈接失敗。Connection refused: connect. 或 Return code is: 405
出現這種錯誤,確定是 POM 文件中配置的 url 錯誤,認真檢查一下 url 是否正確,而後訂正一下,從新部署便可。
4、缺失私服倉庫鏈接配置。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 的您一份參考。
最後,很是感謝親的駐足,但願此文能對親有所幫助。熱烈歡迎親一塊兒探討,共同進步。很是感謝! ^_^