Enhancing java tool usage in azure devops

tarun k 425 Reputation points
2025-06-27T07:29:14.6666667+00:00

I want to enhance pipeline with out using java installer task and also user should have ability to choose respective JDK versions. how we can achieve this

Azure DevOps
0 comments No comments
{count} votes

Accepted answer
  1. Durga Reshma Malthi 6,205 Reputation points Microsoft External Staff Moderator
    2025-06-27T10:37:29.68+00:00

    Hi tarun k

    As we discussed in private message,

    At the top of your BuildPipelinetemplate-dotnet.txt, add this for JDK selection:

    - name: jdk_version
      type: string
      default: '17'
      values:
        - '8'
        - '11'
        - '17'
        - '21'
    

    then replace JavaToolInstaller@0 with a Dynamic Script

    Remove this task:

    User's image

    then Insert this:

    - script: |
        echo "Setting JAVA_HOME for JDK version: ${{ parameters.jdk_version }}"
        case "${{ parameters.jdk_version }}" in
          '8')
            echo "##vso[task.setvariable variable=JAVA_HOME]/usr/lib/jvm/java-8-openjdk-amd64"
            ;;
          '11')
            echo "##vso[task.setvariable variable=JAVA_HOME]/usr/lib/jvm/java-11-openjdk-amd64"
            ;;
          '17')
            echo "##vso[task.setvariable variable=JAVA_HOME]/usr/lib/jvm/java-17-openjdk-amd64"
            ;;
          '21')
            echo "##vso[task.setvariable variable=JAVA_HOME]/usr/lib/jvm/java-21-openjdk-amd64"
            ;;
        esac
      displayName: 'Set JAVA_HOME based on jdk_version'
      shell: bash
    

    In your main pipeline, pass the desired JDK version like this:

    - template: /DevOps/templates/Build/NET Core/build-pipeline-template.yml@JDK
      parameters:
        ...
        jdk_version: 'jdk17'
    

    Hope this helps!

    Please Let me know if you have any queries.


1 additional answer

Sort by: Most helpful
  1. Durga Reshma Malthi 6,205 Reputation points Microsoft External Staff Moderator
    2025-06-27T09:25:06.3166667+00:00

    Hi tarun k

    To enhance an Azure DevOps pipeline for users to select JDK versions without using the Java installer task, consider using pre-installed JDKs on the agent.

    You can specify the JDK version you want to use by passing a parameter to the pipeline:

    parameters:
      - name: jdkVersion
        type: string
        default: '11'
        values:
          - '8'
          - '11'
          - '17'
    jobs:
      - job: Build
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - script: |
              if [ '${{ parameters.jdkVersion }}' == '11' ]; then
                echo "##vso[task.setvariable variable=JAVA_HOME]$JAVA_HOME_11_X64"
              elif [ '${{ parameters.jdkVersion }}' == '8' ]; then
                echo "##vso[task.setvariable variable=JAVA_HOME]$JAVA_HOME_8_X64"
              fi
              echo "##vso[task.setvariable variable=PATH]$JAVA_HOME/bin:$PATH"
            displayName: 'Set JAVA_HOME'
    

    Additional References:

    https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/java-tool-installer-v0?view=azure-pipelines

    Hope this helps!

    Please Let me know if you have any queries.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.