我在部署构件至 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 的您一份参考。
最后,很是感谢亲的驻足,但愿此文能对亲有所帮助。热烈欢迎亲一块儿探讨,共同进步。很是感谢! ^_^