Search code examples
mavenazure-devopsversionpom.xml

Increment version and update pom.xml <version> in Azure DevOps pipeline


I am trying to increment the version number for my builds and then update the pom.xml file with the new version. My Powershell to edit and save the pom.xml does not seem to work, I don't get it to reach the xml.project.version i.e. < version > tag and make changes to it.

Do you have any suggestion how to get it to find the < version > tag and save the updated document?

For additional info, the DevOps pipeline runs in windows-latest at the moment.

trigger:
  branches:
    include:
    - localdev
variables:
- name: azureSubscription
  value: 'xxxx'
- name: webAppName
  value: 'xxx'
- name: environmentName
  value: 'xxx'
- name: vmImageName
  value: 'ubuntu-latest'
- name: version.MajorMinor
  value: '1.0'
- name: version.Patch
  value: $[counter(variables['version.MajorMinor'], 0)]
- name: stableVersionNumber
  value: '$(version.MajorMinor).$(version.Patch)'
- name: prereleaseVersionNumber
  value: 'Set dynamically below in a task'
- name: versionNumber
  value: 'Set dynamically below in a task'
- name: isMainBranch
  value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Fixversionnumber
    displayName: Fix Version number
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: PowerShell@2
      displayName: Set the prereleaseVersionNumber variable value
      inputs:
        targetType: 'inline'
        script: |
          [string] $prereleaseVersionNumber = "$(stableVersionNumber)"
          Write-Host "Setting the prerelease version number variable to '$prereleaseVersionNumber'."
          Write-Host "##vso[task.setvariable variable=prereleaseVersionNumber]$prereleaseVersionNumber"
    - task: PowerShell@2
      displayName: Set the versionNumber to the stable or prerelease version number based on if the 'main' branch is being built or not
      inputs:
        targetType: 'inline'
        script: |
          [bool] $isMainBranch = $$(isMainBranch)
          [string] $versionNumber = "$(prereleaseVersionNumber)"
          if ($isMainBranch)
          {
            $versionNumber = "$(stableVersionNumber)"
          }
          Write-Host "Setting the version number to use to '$versionNumber'."
          Write-Host "##vso[task.setvariable variable=versionNumber]$versionNumber"
    - task: PowerShell@2
      displayName: Set the name of the build (i.e. the Build.BuildNumber)
      inputs:
        targetType: 'inline'
        script: |
          [string] $buildName = "$(versionNumber)_$(Build.SourceBranchName)"
          Write-Host "Setting the name of the build to '$buildName'."
          Write-Host "##vso[build.updatebuildnumber]$buildName"
    - task: PowerShell@2
      displayName: Set the name of the build (i.e. the Build.BuildNumber)
      inputs:
        targetType: 'inline'
        script: |
          #Get version
          $versionNum = "$(versionNumber)"
          # Specify the file path
          #$xmlFileName= "pom.xml"
          # Read the existing file
          [xml]$xml = Get-Content "pom.xml"
          #[xml]$xmlDoc = Get-Content pom.xml
          # If it was one specific element you can just do like so:
          $xml.project.version = "$versionNum"
          #Remove the old pom.xml
          #Remove-Item $xmlFileName
          # Then you can save that back to the xml file
          $xml.Save("$(System.DefaultWorkingDirectory)\pom.xml")
          # Print new file content
          Write-Host "#########################################'$versionNum'.#########################################"
          Write-Host "Setting the version number to use to '$versionNum'."
          Write-Host "######################################### '$versionNum'.#########################################"
          gc $(System.DefaultWorkingDirectory)\pom.xml
    - task: Maven@3
      displayName: 'Maven Package'
      inputs:
        mavenPomFile: 'pom.xml'
    - task: CopyFiles@2
      displayName: 'Copy Files to artifact staging directory'
      inputs:
        SourceFolder: '$(System.DefaultWorkingDirectory)'
        Contents: '**/target/*.?(war|jar)'
        TargetFolder: $(Build.ArtifactStagingDirectory)
    - task: buildDropFile
      inputs:
        targetPath: $(Build.ArtifactStagingDirectory)
        artifactName: drop



Solution

  • You can use a third party extension Replace Tokens. It can search files to find what to replace by a parameter tokenPattern and replaces values with variables in the pipeline. Click "Git if free" and download it to your organization.

    In your pom.xml file, please replace your version number to a unique value, like #{version}#.

    <version>#{versionNumber}#</version>
    

    Please notice the #{...}#, this will help task to find which content to replace. The versionNumber should be the variable that you want to replace.

    Then, in your pipeline, search and add a replace tokens task. Here is an example:

    steps:
    - task: replacetokens@5
      inputs:
        targetFiles: '**/pom.xml'
        encoding: 'auto'
        tokenPattern: 'default' # The defult token pattern is #{...}#
        writeBOM: true
        actionOnMissing: 'continue'
        keepToken: false
        actionOnNoFiles: 'continue'
        enableTransforms: false
        enableRecursion: false
        useLegacyPattern: false
        enableTelemetry: true