使用 Artifactory 插件和 Credentials 插件将 Maven 工件从 Jenkins 部署到 Artifactory 时,如何解决 401 故障?

How can I resolve a 401 failure when deploying a Maven artifact from Jenkins to Artifactory using the Artifactory Plugin and the Credentials Plugin?

我用Jenkins搭建了一个Maven项目

我有 Jenkins Credentials 插件和 Jenkins Artifactory 插件。

我已经在凭据配置中添加了我的 Artifactory 凭据。

我已将 Artifactory 插件配置为使用凭据并已设置服务器 URL。测试连接工作并报告它已找到服务器。

我项目的 POM 在 distributionManagement 部分为 repositorysnapshotsRepository 指定了正确的 Artifactory 路径。

我的 Jenkins 项目构建配置为执行 mvn deploy

在我的本地构建中,我可以从我的开发机器部署到 Artifactory 服务器(我已经在 settings.xml.

中配置了凭据

但是,在我的 Jenkins 工作中,我想使用 Artifactory 插件和 Credentials int he Credentials 插件。当我构建时,我得到以下内容

[DEBUG] Using connector WagonRepositoryConnector with priority 0 for http://xxxxx/artifactory/libs-release-local
Uploading: http://xxxxx/artifactory/libs-release-local/xxx/3.01/xxx-3.01.war
Uploading: http://xxxxx/artifactory/libs-release-local/xxx/3.01/xxx-3.01.pom
Notifying upstream projects of job completion
Join notifier requires a CauseAction
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

和以下堆栈跟踪

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project querynator: 
Failed to deploy artifacts: Could not transfer artifact xxx:war:3.01 from/to dwCentral (http://xxxxx/artifactory/libs-release-local): 
Failed to transfer file: http://xxxxx/artifactory/libs-release-local/xxx/3.01/xxx-3.01.war. 
Return code is: 401, ReasonPhrase: .

这向我表明问题出在凭据上,特别是因为我可以从我的开发箱进行部署(使用 settings.xml 中的凭据),但不能从 Jenkins 进行部署。

我试过部署新版本。我试过部署快照。这不是版本问题。

我该如何解决这个问题?我该如何进一步解决这个问题?

假设您已经正确配置了凭据。如果您已经部署了版本,请对工件说 x.y.z 一次。执行命令

mvn deploy

同样的版本也会导致

Return code is: 401, ReasonPhrase: Unauthorized.

您可以尝试将版本更新到从未部署过的 x.y.a,工作应该 运行 没问题。


更多来自 Maven Lifecycle Basics

的文档

deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

并假设您理解 What exactly is a Maven Snapshot and why do we need it? 并同意这样一个事实,即一旦跨事物共享项目,不应为消费者即时更改,部署 阶段不允许多次用于同一版本。

在 Jenkins 中使用 Artifactory 插件时,不应在主构建任务中使用 mvn deploy。它使用 Maven 部署插件,因此将尝试使用存储在 settings.xml 文件中的凭据。

相反,使用 mvn install 作为主要构建任务,并设置一个新的 post-build 任务 以将工件部署到存储库。

这允许您定义要使用的服务器、存储库位置和凭据。