We can use the extension Replace Tokens or Magic Chunks to update the pom.xml file.
pom.xml content:
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>JAVAHOME</jdkHome>
</configuration>
</toolchain>
Step:
a. Create a build pipeline A, Open the build pipeline A and add a new variable JAVAHOME and grant test Build Service (xxx) account the Edit build pipeline permission. (open the build pipeline(A)--> ... --> Security --> Edit build pipeline set to Allow)
b. enable the feature Allow scripts to access the OAuth token (Click Agent Job Name=>Additional options) add task powershell(update the JAVAHOME value) and enter the script below.
$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/{build pipeline A definition ID}?api-version=5.1"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
# Update an existing variable named JAVAHOME to its new value JAVA_HOME_11_X64
$pipeline.variables.JAVAHOME.value= $($env:JAVA_HOME_11_X64)
# my sample is get the variable JAVA_HOME_11_X64, please update it to JAVA_HOME_8_X64
####****************** update the modified object **************************
$json = $($pipeline | ConvertTo-Json -Depth 100)
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "=========================================================="
Write-host "The value of Varialbe 'JAVAHOME' is updated to" $updatedef.variables.JAVAHOME.value
write-host "=========================================================="
c. Add task Replace Tokens and configure the task.
d. Add a task powershell and use the code $ourfilesdata = Get-Content "pom.xml"
Write-Output $ourfilesdata
to output the file content.
Result: