Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4X

朱雀 2023-05-31 15:30 460阅读 0赞

我在部署构件至 maven nexus 私服时,有时会出现 Failed to deploy artifacts: Failed to transfer file: … Return code is: 4XX, ReasonPhrase: … 类似这样的错误,那么这些错误是怎么产生,又如何解决呢?我在此将自己在部署过程中遇到的错误整理汇总一下,供大家参阅,希望对大家有所帮助。

  一、错误的请求。Return code is: 400, ReasonPhrase: Bad Request.

  watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p6dWNoZW55Yg_size_16_color_FFFFFF_t_70

  具体错误信息如下所示:  

复制代码

  1. [INFO] ------------------------------------------------------------------------
  2. [INFO] BUILD FAILURE
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Total time: 5.135 s
  5. [INFO] Finished at: 2016-02-18T10:23:58+08:00
  6. [INFO] Final Memory: 19M/174M
  7. [INFO] ------------------------------------------------------------------------
  8. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger:
  9. Failed to deploy artifacts: Could not transfer artifact nl.flotsam:xeger:jar:1.0.2 from/to nexus-releases
  10. (http://localhost:8081/nexus/content/repositories/releases/):
  11. Failed to transfer file: http://localhost:8081/nexus/content/repositories/releases/nl/flotsam/xeger/1.0.2/xeger-1.0.2.jar.
  12. Return code is: 400, ReasonPhrase: Bad Request. -> [Help 1]
  13. [ERROR]
  14. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  15. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  16. [ERROR]
  17. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  18. [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 改为 “” 即可解决,解决方法如下所示:

  watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p6dWNoZW55Yg_size_16_color_FFFFFF_t_70 1

  

  二、未认证或认证不通过。Return code is: 401, ReasonPhrase: Unauthorized.

  watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p6dWNoZW55Yg_size_16_color_FFFFFF_t_70 1

  1. [INFO] ------------------------------------------------------------------------
  2. [INFO] BUILD FAILURE
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Total time: 2.215 s
  5. [INFO] Finished at: 2016-02-18T10:50:56+08:00
  6. [INFO] Final Memory: 19M/175M
  7. [INFO] ------------------------------------------------------------------------
  8. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger:
  9. Failed to deploy artifacts: Could not transfer artifact nl.flotsam:xeger:jar:1.0.2 from/to nexus-releases
  10. (http://localhost:8081/nexus/content/repositories/releases/): Failed to transfer file:
  11. http://localhost:8081/nexus/content/repositories/releases/nl/flotsam/xeger/1.0.2/xeger-1.0.2.jar.
  12. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
  13. [ERROR]
  14. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  15. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  16. [ERROR]
  17. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  18. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

  出现这种错误,原因如下所示:

  1、是未配置 Nexus 私服用户信息,或配置的账号、密码错误,均会出现 401 的错误,解决方法就是修改 maven settings.xml 配置文件,添加如下信息即可解决问题。  

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. ...
  6. <servers>
  7. <!-- Nexus 构件部署用户信息 -->
  8. <server>
  9. <id>nexus-releases</id>
  10. <username>fanfengping</username>
  11. <password>密码</password>
  12. </server>
  13. <server>
  14. <id>nexus-snapshots</id>
  15. <username>fanfengping</username>
  16. <password>密码</password>
  17. </server>
  18. </servers>
  19. ...
  20. </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

  具体提示信息如下所示:

  1. [INFO] ------------------------------------------------------------------------
  2. [INFO] BUILD FAILURE
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Total time: 1.969 s
  5. [INFO] Finished at: 2016-02-18T10:47:22+08:00
  6. [INFO] Final Memory: 18M/177M
  7. [INFO] ------------------------------------------------------------------------
  8. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger:
  9. Deployment failed: repository element was not specified in the POM inside distributionManagement element
  10. or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
  11. [ERROR]
  12. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  13. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  14. [ERROR]
  15. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  16. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

  此问题是因 POM 文件遗漏了 Nexus 私服连接配置导致的,在 POM 文件中添加 distributionManagement 节点或者在命令行执行时添加 -DaltDeploymentRepository=id::layout::url 参数即可解决。POM 文件中添加 distributionManagement 如下所示:

  1. <project>
  2. ...
  3. <developers>
  4. ...
  5. </developers>
  6. <distributionManagement>
  7. <repository>
  8. <id>nexus-releases</id>
  9. <name>Nexus Releases Repository Pro</name>
  10. <url>http://localhost:8081/nexus/content/repositories/releases/</url>
  11. </repository>
  12. <snapshotRepository>
  13. <id>nexus-snapshots</id>
  14. <name>Nexus Snapshots Repository Pro</name>
  15. <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
  16. </snapshotRepository>
  17. </distributionManagement>
  18. <dependencies>
  19. ...
  20. </dependencies>
  21. <build>
  22. ...
  23. </build>
  24. </project>

至此,Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: … Return code is: 4XX, ReasonPhrase: … 解决方案 顺利完结,希望此文能够给初学 Maven 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

发表评论

表情:
评论列表 (有 0 条评论,460人围观)

还没有评论,来说两句吧...

相关阅读