使用 Azure Pipelines 發佈 Maven 成品

您可以使用 Azure Pipelines,將 Maven 套件發佈至 Azure Artifacts 摘要、公用登錄或作為管線成品。

設定您的專案

  1. 將下列代碼段新增至 repositories pom.xml 檔案中的 distributionManagement 區段。 以組織名稱、專案名稱和摘要名稱取代預留位置。

    <repository>
      <id>MavenDemo</id>
      <url>https://pkgs.dev.azure.com/ORGANIZATION-NAME/PROJECT-NAME/_packaging/FEED-NAME/maven/v1</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    
  2. 設定您的 settings.xml 檔案,如下所示。 以您的組織名稱、專案名稱和您的個人存取令牌取代佔位元。

    <server>
      <id>PROJECT-NAME</id>
      <username>ORGANIZATION-NAME</username>
      <password>PERSONAL-ACCESS-TOKEN</password>
    </server>
    
  3. 使用封裝讀取和寫入範圍建立個人存取令牌,並將它貼到 password settings.xml 檔案中的標記中。

建置您的程式碼

在此範例中 ,我們使用 Maven 工作 來使用 Azure Pipelines 建置專案。

- task: Maven@3
  inputs:
    mavenPomFile: 'my-app/pom.xml'    // Path to your pom file
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'package'

將套件發佈至管線

下列範例說明如何將成品發佈至 管線中卸 除。 使用 [ 複製檔案 ] 工作將套件複製到目標資料夾,然後使用 [發佈組建成品 ] 將組建成品發佈至 Azure Pipelines。

- task: CopyFiles@2
  inputs:
    Contents: '**'
    TargetFolder: '$(build.artifactstagingdirectory)'
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

A screenshot showing the build artifact published to drop in Azure Pipelines.

將套件發佈至您的摘要

若要將套件發佈至 Azure Artifacts 摘要,請使用 Maven 工作將您的成品部署至摘要。

- task: Maven@3
  inputs:
    mavenPomFile: 'my-app/pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    mavenAuthenticateFeed: true
    publishJUnitResults: false
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'deploy'

A screenshot showing the build artifact published to a feed.

問與答

問:如何使用 MavenAuthenticate 進行驗證?

答:您可以使用 MavenAuthenticate 工作 ,向組織內外的 Maven 摘要進行驗證。 如需詳細資訊,請參閱下列範例: