0
votes

For my java project, I am using azure devops pipeline for building. For configuring the jdkhome path, I am putting it in the toolchains xml file placed in root folder of project :

<toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>oracle</vendor>
    </provides>
    <configuration>
      <!--<jdkHome>/usr/lib/jvm/zulu-8-azure-amd64</jdkHome>-->
    </configuration>
  </toolchain>

But the azure devops agent has updated their jdk and its failing my build. So, now rather than hardcoding, I want to pick the path from agent. I see that on agent, environment variable is set as $JAVA_HOME_8_X64 with the path assigned to it.

So, how can I use this environment variable in my pipeline weather refer it in toolschain xml file or in some other way?

I tried to directly refer in xml and it did not work. I also tried to add some tasks from marketplace but did not work. Can someone help me with this?

1
Why do you need a toolchain file? Why don't you build with higher JDK, but for Java 8?J Fabian Meier
@JFabianMeier I tried commenting everything in toolchain file and make the pipelihigher JDK from agent but it failed. can you point out how to do that?Sushil
So which problem did you encounter?J Fabian Meier
@JFabianMeier .. I am not sure what and where should I set so that it picks the jdk version defined by agent with JAVA_HOME_8_X64 variable.Sushil
Hi Sushil, not get your latest information, are the answers below helpful for you? Or if you have any concern, feel free to share it here, we will help you.Vito Liu

1 Answers

0
votes

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) enter image description here

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. enter image description here

d. Add a task powershell and use the code $ourfilesdata = Get-Content "pom.xml" Write-Output $ourfilesdata to output the file content.

Result: enter image description here