javamavenazure-devopsazure-pipelinesazure-pipelines-yaml

Azure DevOps JavaToolInstaller Error/Issue with JDK versions


Trying to implement a pipeline with Azure DevOps for springboot maven 4.0 on jdk17. There is an issue where the pipeline is incompatible with one of the bash commands as it wants to run jdk11. I am trying to use JavaToolInstaller to update the system to jdk17, but get an error saying the plugin is unsupported even with code to install it.

.yml code

trigger:
  - master

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        # Install Java 17 manually
        sudo apt-get update
        sudo apt-get install -y openjdk-17-jdk
        
        # Verify Java version
        java -version

  - task: Maven@4
    inputs:
      mavenPomFile: 'pom.xml'
      mavenOptions: '-Xmx1024m'
      javaHomeOption: 'Path'
      jdkDirectory: '/usr/lib/jvm/java-17-openjdk-amd64'
      goals: 'clean package'

  # Copy JAR files to the staging directory
  - task: CopyFiles@2
    inputs:
      contents: '**/target/*.jar'
      targetFolder: '$(Build.ArtifactStagingDirectory)'

  # Publish build artifacts
  - task: PublishBuildArtifacts@1
    inputs:
      pathToPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: 'drop'

  # Run the Spring Boot application
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        # Verify Java 17 is being used
        java -version

        echo "Listing files in target directory:"
        ls -al $(Build.ArtifactStagingDirectory)/target

        echo "Running the Spring Boot application..."
        java -jar $(Build.ArtifactStagingDirectory)/target/ASD-0.0.1-SNAPSHOT.jar

Bash Error:

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.244.1
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash /home/vsts/work/_temp/96124f0c-8298-4b37-815e-c653e4b5db03.sh
openjdk version "11.0.24" 2024-07-16
OpenJDK Runtime Environment Temurin-11.0.24+8 (build 11.0.24+8)
OpenJDK 64-Bit Server VM Temurin-11.0.24+8 (build 11.0.24+8, mixed mode)
Listing files in target directory:
total 32756
drwxr-xr-x 2 vsts docker     4096 Sep  5 12:23 .
drwxr-xr-x 3 vsts docker     4096 Sep  5 12:23 ..
-rw-r--r-- 1 vsts docker 33533344 Sep  5 12:23 ASD-0.0.1-SNAPSHOT.jar
Running the Spring Boot application...
Error: LinkageError occurred while loading main class org.springframework.boot.loader.launch.JarLauncher
    java.lang.UnsupportedClassVersionError: org/springframework/boot/loader/launch/JarLauncher has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0

##[error]Bash exited with code '1'.
Finishing: Bash

JavaToolInstaller error:

Starting: JavaToolInstaller
==============================================================================
Task         : Java tool installer
Description  : Acquire a specific version of Java from a user-supplied Azure blob or the tool cache and sets JAVA_HOME
Version      : 0.245.3
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/tool/java-tool-installer
==============================================================================
Cleaning destination folder before extraction: /home/vsts/work/1/s
Retrieving the JDK from local path.
##[error]Specified JDK source file does not have a supported file extension.
##[error]Specified JDK source file does not have a supported file extension.
Finishing: JavaToolInstaller

I have tried a few implementations to update the system already, but the Java Version print after the update indicated no change.


Solution

  • Both Java 11 and Java 17 are pre-installed into the ubuntu-latest build agent:

    Pre-installed Java versions

    This means you can use the JavaToolInstaller@0 using the jdkSourceOption: 'PreInstalled' as follows:

    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
      - checkout: none
    
      - task: JavaToolInstaller@0
        displayName: 'Install Java 17'
        inputs:
          versionSpec: '17'
          jdkArchitectureOption: 'x64'
          jdkSourceOption: 'PreInstalled'
      
      - script: |
          java -version
        displayName: 'Check Java 17 version'
    
      - task: JavaToolInstaller@0
        displayName: 'Install Java 11'
        inputs:
          versionSpec: '11'
          jdkArchitectureOption: 'x64'
          jdkSourceOption: 'PreInstalled'
      
      - script: |
          java -version
        displayName: 'Check Java 11 version'
    

    Running the pipeline:

    Install Java 17 - task logs

    Check Java 17 - task logs

    Logs regarding the Java 11 tasks are similar.