You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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!
Activity
[-]Maven Toolchains: clarify tool SDK setup[/-][+]Maven Toolchains: clarify tool JDK setup[/+]v-priyagupta108 commentedon May 19, 2025
Hello @stechio 👋,
Thank you for your report. We'll investigate the issue and get back to you with the details!
stechio commentedon May 20, 2025
Apparently,
JAVA_HOMEis updated at every JDK added, so the last becomes the one Maven runs under; the following configuration makes the workflow succeed:Please, update your documentation to explicitly state that the last JDK added is the active one.
v-chiranjib-swain commentedon May 26, 2025
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_HOMEandPATHenvironment variables at the appropriate stages. Here's the correct and recommended way to achieve this using GitHub Actions:Recommended Workflow
Key Notes
JAVA_HOMEand and prepend its bin directory to thePATHto ensure the desired JDK takes priority during execution.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!
stechio commentedon May 26, 2025
Thanks for your support, @chiranjib-swain !