Maven-008-Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 解决方案

  我在部署构件至 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 的您一份参考。 

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

 

 

 

posted @ 2016-02-18 13:25  范丰平  Views(42204)  Comments(8Edit  收藏  举报