Skip to content

Maven Toolchains: clarify tool JDK setup #835

Closed

Description

@stechio

I need to run Maven under JDK 17, while building source code under JDK 11.

The documentation explains how to set up the toolchain, but doesn't seem to indicate how to specify the JDK under which Maven itself has to run.

Could you please clarify the proper way to specify the JDK (in my case, 17) for running Maven, as I select multiple JDKs for toolchain? Do I have to explicitly set JAVA_HOME after selecting the JDKs? What else?

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-version: |
          11
          17

    # How do I make `mvn` run under JDK 17?
    # Is there a declarative statement to add?
    # Do I have to explicitly set `JAVA_HOME`?
    # What else?

    - name: Build with Maven
      run: mvn -B package --file pom.xml

Activity

  1. changed the title [-]Maven Toolchains: clarify tool SDK setup[/-] [+]Maven Toolchains: clarify tool JDK setup[/+] on May 17, 2025
  2. v-priyagupta108 commented on May 19, 2025

    @v-priyagupta108
    Contributor

    Hello @stechio 👋,
    Thank you for your report. We'll investigate the issue and get back to you with the details!

  3. stechio commented on May 20, 2025

    @stechio
    Author

    Apparently, JAVA_HOME is updated at every JDK added, so the last becomes the one Maven runs under; the following configuration makes the workflow succeed:

    name: Java CI with Maven
    
    on: [push, pull_request]
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v4
        - name: Set up build JDK
          uses: actions/setup-java@v4
          with:
            java-version: '11'
            distribution: 'temurin'
        - name: Set up tool JDK
          uses: actions/setup-java@v4
          with:
            java-version: '17'
            distribution: 'temurin'
            cache: maven
        # `mvn` runs under JDK 17
        - name: Build with Maven
          run: mvn --batch-mode verify
    

    Please, update your documentation to explicitly state that the last JDK added is the active one.

  4. v-chiranjib-swain commented on May 26, 2025

    @v-chiranjib-swain

    Hi @stechio
    Thank you for raising this! To ensure Maven runs under JDK 17 while building or running the source code with JDK 11, you’ll need to explicitly configure both the JAVA_HOME and PATH environment variables at the appropriate stages. Here's the correct and recommended way to achieve this using GitHub Actions:

    Recommended Workflow

    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v4
    
        - uses: actions/setup-java@v4
          with:
            distribution: 'temurin'
            java-version: |
              11
              17
    
        # Set JAVA_HOME and PATH for Maven (JDK 17)
        - name: Set JAVA_HOME and PATH for Maven
          run: |
            echo "JAVA_HOME=${JAVA_HOME_17_X64}" >> $GITHUB_ENV
            echo "${JAVA_HOME_17_X64}/bin" >> $GITHUB_PATH
    
        - name: Build with Maven
          run: mvn -B package --file pom.xml
    
        # Set JAVA_HOME and PATH for Source Code (JDK 11)
        - name: Set JAVA_HOME and PATH for Source Code
          run: |
            echo "JAVA_HOME=${JAVA_HOME_11_X64}" >> $GITHUB_ENV
            echo "${JAVA_HOME_11_X64}/bin" >> $GITHUB_PATH
    
        - name: Build Source Code
          run: java -version
    

    Key Notes

    • The setup-java action allows installing multiple JDK versions, but only the last one listed is active by default as per the documentation here.
    • To override this, explicitly set JAVA_HOME and and prepend its bin directory to the PATH to ensure the desired JDK takes priority during execution.
    • This approach ensures Maven runs under JDK 17 while the source code is built under JDK 11.

    Related Pull Request
    I’ve raised a PR to update the documentation with these details for better clarity: PR #841
    Let me know if you need help adapting this further to your workflow!

  5. stechio commented on May 26, 2025

    @stechio
    Author

    Thanks for your support, @chiranjib-swain !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions