首页 > 解决方案 > 无法验证 Maven 以从 ADO 管道访问 Azure 工件

问题描述

我有一个在云中运行的天蓝色管道,我基本上想在其中运行 Blackduck 扫描。为了简单起见,blackduck 任务运行一个 maven 命令来构建依赖关系树。为此,它会扫描我的 pom.xml 中的所有依赖项。

我目前在 Azure DevOps 中有 2 个提要。1 个存储外部库的提要和 1 个存储内部开发的内部库的提要。所有外部依赖项,maven 命令都可以检索,但不能检索内部依赖项。我不断收到 401 Unauthorized 错误。

我已更新我的 pom.xml 以包含连接到 azure artifact 的凭据,如此处所示。我还更新了 settings.xml 文件以包含连接。在运行 maven 命令之前,我已经添加了 Maven 身份验证任务

Yml管道:

- task: PowerShell@2
      displayName: "Modify Settings.xml"
      inputs:
        targetType: 'inline'
        script: |
          $xmlPom = [xml]"<server>
                <id>Pack-All</id>
                <username>Bob</username>
                <password>$(ado.pat)</password>
              </server>"
              
          $file = "settings.xml"
          $origin = "$(M2_HOME)\conf"

          $xdoc = new-object System.Xml.XmlDocument

          $fileXml = resolve-path(“$origin\$file”)

          $xdoc.load($fileXml)

          $xdoc.settings.servers.AppendChild($xdoc.ImportNode($xmlPom.server, $true))

          $xdoc = [xml] $xdoc.OuterXml.Replace(" xmlns=`"`"", "")

          $xdoc.Save(“$origin\$file”)
- task: MavenAuthenticate@0
      displayName: 'Maven Authenticate'
      inputs:
        artifactsFeeds: 'Pack-All'
- task: SynopsysDetectTask@2  
      displayName: "Run Black Duck analysis"
      condition: and(succeeded(), eq('${{ parameters.blackduck }}', 'true'))
      continueOnError: true
      inputs:
        Products: 'BD'
        BlackDuckService: 'Black Duck'
        DetectVersion: 'latest'
        DetectArguments: '--detect.project.name=$(Build.Repository.Name)Test --detect.binary.scan.file.path=$(Build.SourcesDirectory)\app.war --detect.maven.build.command=-DmavenFeedAuthenticate=true'

Blackduck 任务执行的 Maven 命令:

C:\ProgramData\chocolatey\lib\maven\apache-maven-3.6.3\bin\mvn.cmd -DmavenFeedAuthenticate=true dependency:tree -T1

错误日志:

[ERROR] Failed to execute goal on project dimload-ms-app-agg: Could not resolve dependencies for project ca.test-ms-app-agg:war:0.0.1-SNAPSHOT: Failed to collect dependencies at ca.cn.boot:helpers:jar:0.4.28950: Failed to read artifact descriptor for ca.test.boot:helpers:jar:0.4.28950: Could not transfer artifact ca.test.boot:helpers:pom:0.4.28950 from/to Test-All (https://pkgs.dev.azure.com/Test-Int/_packaging/Pack-All/maven/v1): Authentication failed for https://pkgs.dev.azure.com/Test-Int/_packaging/Pack-All/maven/v1/ca/test/boot/helpers/0.4.28950/helpers-0.4.28950.pom 401 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/DependencyResolutionException

标签: javamavenazure-devops

解决方案


从您的 Yaml 示例中,您已添加MavenAuthenticate任务。此任务将自动为目标提要生成 settings.xml。

该文件存在于路径中xxx\.m2\settings.xml 在此处输入图像描述

所以你不需要settings.xml手动添加这个文件()。这也可以帮助您简化代码。

https://pkgs.dev.azure.com/...的身份验证失败。401未经授权

此问题的可能原因是构建服务帐户没有足够的提要权限。

您可以尝试以下步骤:

  1. 导航到Artifacts ->Target Feed ->Feed Settings -> Permission

  2. 将 Project Build Service( ProjectName Build Service(OrganizationName)) 设置为目标提要中的贡献者角色。

在此处输入图像描述

或者您可以启用该选项Allow Project-Scoped Builds

在此处输入图像描述

另一方面,从提要 URL 来看,它似乎是一个组织范围提要。

您可以检查Limit job authorization scope to current project for non-release pipelines选项是否EnabledProject Settings -> Pipelines.

您可以尝试禁用该选项。

注意:要禁用此选项,您需要先禁用该选项Organization Settings-> Settings。然后您可以在项目级别禁用该选项。


推荐阅读