Depending on the image used by the build, JDK21 may already be installed. See e.g. the Java versions available in the Ubuntu-20.04 image.
So the only thing required is to use e.g JavaToolInstaller
to specify the version you want to use:
- task: JavaToolInstaller@0
inputs:
versionSpec: '21'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
and then specify this version in the Gradle task:
- task: Gradle@3
inputs:
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.21'
jdkArchitectureOption: 'x64'
tasks: 'clean test'
See also: Is it possible to use JDK 21 in an Azure Pipeline Gradle task?