Selecting Java versions 17 and 21 and testing the pipeline against agent which is having 17 and 21 JDK versions

Shyam Kumar 846 Reputation points
2025-04-24T03:28:12.0766667+00:00

We have extracted Java 17 and Java 21 JDK versions and set up environment variables for both on the Linux agent pool. Now, we want to test the build process using Maven and Gradle tasks. To achieve this, we need to create a build pipeline that can easily run Maven and Gradle builds. Additionally, we require a test repository to validate the setup by running builds with both Java 17 and Java 21 against the agent pool. Could you provide the necessary tasks and suggest a test repository for this validation?

Azure DevOps
{count} votes

Accepted answer
  1. Pravallika Kothaveeranna Gari 955 Reputation points Microsoft External Staff Moderator
    2025-04-29T09:01:24.8433333+00:00

    Hi Diptesh Kumar,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!

    Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Update:

    To resolve the error The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program:

    We need to explicitly set the environment variable for JAVA_HOME to a specific folder-level path, but only for that particular task.


    To Summarize, you can build a pipeline that switches between Java versions using environment variables and run both Maven and Gradle builds.

    Sample Pipeline:

    
    trigger:
    
      branches:
    
        include:
    
          - main
    
    pool:
    
      vmImage: 'ubuntu-latest'
    
    variables:
    
      JAVA_HOME_17: '/path/to/java17'
    
      JAVA_HOME_21: '/path/to/java21'
    
    jobs:
    
      - job: MavenBuild
    
        displayName: 'Maven Build'
    
        steps:
    
          - task: UseJavaVersion@1
    
            inputs:
    
              versionSpec: '17'
    
              jdkArchitectureOption: 'x64'
    
              jdkSourceOption: 'PreInstalled'
    
              jdkHome: $(JAVA_HOME_17)
    
          - script: |
    
              mvn clean install
    
            displayName: 'Run Maven Build'
    
      - job: GradleBuild
    
        displayName: 'Gradle Build'
    
        steps:
    
          - task: UseJavaVersion@1
    
            inputs:
    
              versionSpec: '21'
    
              jdkArchitectureOption: 'x64'
    
              jdkSourceOption: 'PreInstalled'
    
              jdkHome: $(JAVA_HOME_21)
    
          - script: |
    
              ./gradlew build
    
            displayName: 'Run Gradle Build'
    
    

    You can use the GitHub repository https://github.com/spring-projects/spring-petclinic.git which contains both Maven and Gradle build configurations to achieve your requirement.

    If you want to validate the Maven installation and run the build tasks using Java 17 and Java 21 versions, use mavenPath: '$(MAVEN_HOME)' instead of mentioning mavenPath: '$(MAVEN_HOME)'/bin.

    This is to avoid bin/mvn getting appended redundantly which causes the path to be incorrect like /apache-maven-version/bin/bin/mvn and prevents path-related errors during build process:

    
    variables:
    
      MAVEN_HOME: '/AzDOAgents/apache-maven-3.8.8' # Correct Maven installation path
    
    stages:
    
    - stage: ValidateEnvironment
    
      displayName: 'Validate Environment Setup'
    
      jobs:
    
      - job: ValidateMaven
    
        displayName: 'Validate Maven Installation'
    
        steps:
    
        # Step 1: Validate Maven Installation
    
        - script: |
    
            echo "### Validating Maven Installation ###"
    
            echo "MAVEN_HOME is set to: $MAVEN_HOME"
    
            export PATH=$MAVEN_HOME/bin:$PATH
    
            which mvn
    
            mvn -version
    
          displayName: 'Validate MAVEN_HOME Path'
    
        # Step 2: Run Maven Task
    
        - task: Maven@4
    
          inputs:
    
            mavenPomFile: 'pom.xml'
    
            publishJUnitResults: false
    
            javaHomeOption: 'JDKVersion'
    
            jdkVersionOption: '11'  
    
            mavenVersionOption: 'Path'  
    
            mavenPath: '$(MAVEN_HOME)' 
    
            mavenAuthenticateFeed: false
    
            effectivePomSkip: false
    
            sonarQubeRunAnalysis: false
    
          displayName: 'Run Maven'
    
    

    Hope this helps.


    Please do not forget to click "Accept the answer” and Yes wherever the information provided helps you, this can be beneficial to other community members.

    User's image

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.


0 additional answers

Sort by: Most helpful

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.