javagradleazure-pipelines

Is it possible to use JDK 21 in an Azure Pipeline Gradle task?


My Azure pipeline contains the following task

  - task: Gradle@2
    displayName: 'Run checks and tests'
    inputs:
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '1.17'
      tasks: 'check jacocoTestCoverageVerification jacocoTestReport'

I would like to upgrade to JDK v21, but if I replace 1.17 with 1.21 I get the following error:

Error: Failed to find the specified JDK version. Please ensure the specified JDK version is installed on the agent and the environment variable 'JAVA_HOME_21_X64' exists and is set to the location of a corresponding JDK or use the Java Tool Installer task to install the desired JDK.

I also tried using using Gradle@3 with jdkVersionOption: '1.21', but it fails with the same error message


Solution

  • I can reproduce the same issue when using the Java21 in Azure Pipeline.

    The root cause of the issue is that the Java 21 is not pre-installed in Pipeline agent.

    Is it possible to use JDK 21 in an Azure Pipeline Gradle task?

    The answer is YES. We can add steps to install the Java 21 before Gradle build.

    Refer to the following YAML sample:

    Windows Agent:

    steps:
    - powershell: |
       $source = "https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.zip"
       $destination = "$(build.sourcesdirectory)\jdk-21_windows-x64_bin.zip"
       $client = new-object System.Net.WebClient 
       $cookie = "oraclelicense=accept-securebackup-cookie"
       $client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie) 
       $client.downloadFile($source, $destination)
      displayName: 'PowerShell Script'
    
    - task: JavaToolInstaller@0
      displayName: 'Use Java 21'
      inputs:
        versionSpec: 21
        jdkArchitectureOption: x64
        jdkSourceOption: LocalDirectory
        jdkFile: '$(build.sourcesdirectory)\jdk-21_windows-x64_bin.zip'
        jdkDestinationDirectory: '$(agent.toolsDirectory)/jdk21'
    
    - task: Gradle@2
      inputs:
        workingDirectory: ''
        gradleWrapperFile: 'gradlew'
        gradleOptions: '-Xmx3072m'
        javaHomeOption: 'JDKVersion'
        jdkVersionOption: '1.21'
        jdkArchitectureOption: 'x64'
        publishJUnitResults: true
        testResultsFiles: '**/TEST-*.xml'
        tasks: 'build'
    

    Linux Agent:

    steps:
    - bash: 'wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz '
      displayName: 'Bash Script'
    
    - task: JavaToolInstaller@0
      displayName: 'Use Java 21'
      inputs:
        versionSpec: 21
        jdkArchitectureOption: x64
        jdkSourceOption: LocalDirectory
        jdkFile: '$(build.sourcesdirectory)/jdk-21_linux-x64_bin.tar.gz'
        jdkDestinationDirectory: '$(agent.toolsDirectory)/jdk21'
    
    - task: Gradle@2
      inputs:
        workingDirectory: ''
        gradleWrapperFile: 'gradlew'
        gradleOptions: '-Xmx3072m'
        javaHomeOption: 'JDKVersion'
        jdkVersionOption: '1.21'
        jdkArchitectureOption: 'x64'
        publishJUnitResults: true
        testResultsFiles: '**/TEST-*.xml'
        tasks: 'build'
    

    The script will download the Java 21 Java Official site. And the Pipeline task: Java Installer to configure the agent environemnt.