Published on

Managing Multiple Java Versions in Azure DevOps Pipeline on Windows Server 2019 with Visual Studio 2019 Build Agent



The default Java version of Windows Server 2019 with Visual Studio 2019 is Java 8. So, when we need to build a Java application with Java 21 we need to use Java 21 in the build server.


Windows Server 2019 with Visual Studio 2019 server image already has following Java versions.


Version Environment Variable
8.0.412+8 (default) JAVA_HOME_8_X64
11.0.23+9 JAVA_HOME_11_X64
17.0.11+9 JAVA_HOME_17_X64
21.0.3+9.0 JAVA_HOME_21_X64

In the Azure Devops pipeline YAML file, we need to set Java 21.

- script: |
echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_21_X64)"
echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_21_X64)\bin;$(PATH)"
java -version
displayName: "Set JDK to 21"

This enables us to use Java 21. Further, we can use parameters to allow multiple java versions to used.

Further, if we are using the Maven build task we need to explicitly mention the jdkVersion as input parameter to Maven Action.


- task: Maven@3
displayName: 'Maven pom.xml'
mavenPomFile: 'pom.xml'
goals: 'clean verify -P ${{parameters.environment}}'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.21'
jdkArchitectureOption: 'x64'
publishJUnitResults: false
mavenAuthenticateFeed: true