How can I compile both main
and test
sources with my system's default Java version (e.g. Java 8.0 specified in $JAVA_HOME
), but use a different Java installation to execute the JUnit tests (e.g. Java 11.0) in Maven?
This question is different from some previous questions, which want to use different target source versions, or different compilers for the main
and test
sources:
- Compile Maven Module with Different Java Version
- Different maven compiler versions for test and main
I want to use the same compiler (e.g. Java 8.0) for both compile
and test-compile
goals of Maven, but execute all JUnit tests of Maven's test
goal via another Java installation (e.g. Java 11.0).
I'm using maven-surefire
for execution of unit-tests.
I'm looking for a short and clean solution, and I'd rather not use maven-toolchains
.
But if using maven-toolchains
is the only possible solution, then so be it.