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